Challenge #2: Stopping
The build is timing out because the server is running. In this challenge, you’ll add code to stop the server.
Instructions
1. Revise the code in the "performs request"
test:
- Stop the HTTP server. The build should no longer time out.
- Log
"SERVER CLOSED"
when theclose
event occurs. - Use
await new Promise(...)
to wait for the server to stop. - Log
"SERVER STOPPED"
after the promise resolves.
2. Check your test output. You should see:
SERVER LISTENING
SERVER STARTED
SERVER CLOSED
SERVER STOPPED
Remember to commit your changes when you’re done.
API Documentation
server.close();
Stop the server by listening for incoming HTTP requests on the specified port. This function returns immediately, before the server has stopped. To wait for the server to stop, wait for the close
event.
server.on("close", fn);
Run fn
after the server has finished stopping.
- fn
(() => void)
- the function to run
JavaScript Primers
No new concepts.
Hints
1
You’ll need access to the server
variable.
You can move it out of the promise.
it("performs request", async () => {
const server = http.createServer();
await new Promise((resolve, reject) => { // add <void> for TypeScript
server.listen(PORT);
server.on("listening", () => {
console.log("LISTENING");
return resolve();
});
});
console.log("SERVER STARTED");
});
2
The rest of this challenge is almost identical to the previous challenge.
Begin by telling the server to stop.
You can do that with server.close()
.
The build should no longer time out once you do this.
it("performs request", async () => {
const server = http.createServer();
await new Promise((resolve, reject) => { // add <void> for TypeScript
server.listen(PORT);
server.on("listening", () => {
console.log("SERVER LISTENING");
return resolve();
});
});
console.log("SERVER STARTED");
server.close();
});
3
Log the close
event.
You can do that with server.on("close", ...)
.
You’ll see that the test output is slightly skewed. That’s because the test is exiting before the server shuts down.
it("performs request", async () => {
const server = http.createServer();
await new Promise((resolve, reject) => { // add <void> for TypeScript
server.listen(PORT);
server.on("listening", () => {
console.log("SERVER LISTENING");
return resolve();
});
});
console.log("SERVER STARTED");
server.close();
server.on("close", () => {
console.log("SERVER CLOSED");
});
});
4
Wait for the server to stop, then log.
You can use await new Promise()
.
If the test times out, it’s because you forgot to call resolve()
.
The test output should be back to normal, because the test is no longer exiting before the server shuts down.
it("performs request", async () => {
const server = http.createServer();
await new Promise((resolve, reject) => { // add <void> for TypeScript
server.listen(PORT);
server.on("listening", () => {
console.log("SERVER LISTENING");
return resolve();
});
});
console.log("SERVER STARTED");
await new Promise((resolve, reject) => { // add <void> for TypeScript
server.close();
server.on("close", () => {
console.log("SERVER CLOSED");
return resolve();
});
});
console.log("SERVER STOPPED");
});
Complete Solution
Test code (JavaScript):
it("performs request", async () => {
const server = http.createServer();
await new Promise((resolve, reject) => {
server.listen(PORT);
server.on("listening", () => {
console.log("SERVER LISTENING");
return resolve();
});
});
console.log("SERVER STARTED");
await new Promise((resolve, reject) => {
server.close();
server.on("close", () => {
console.log("SERVER CLOSED");
return resolve();
});
});
console.log("SERVER STOPPED");
});
Test code (TypeScript):
it("performs request", async () => {
const server = http.createServer();
await new Promise<void>((resolve, reject) => {
server.listen(PORT);
server.on("listening", () => {
console.log("SERVER LISTENING");
return resolve();
});
});
console.log("SERVER STARTED");
await new Promise<void>((resolve, reject) => {
server.close();
server.on("close", () => {
console.log("SERVER CLOSED");
return resolve();
});
});
console.log("SERVER STOPPED");
});
No production code yet.