2.2 KiB
2.2 KiB
id | title | challengeType |
---|---|---|
587d7fb0367417b2b2512bee | Start a Working Express Server | 2 |
Description
app.listen(port)
. It tells your server to listen on a given port, putting it in running state. You can see it at the bottom of the file. It is inside comments because for testing reasons we need the app to be running in background. All the code that you may want to add goes between these two fundamental parts. Glitch stores the port number in the environment variable process.env.PORT
. Its value is 3000
.
Let’s serve our first string! In Express, routes takes the following structure: app.METHOD(PATH, HANDLER)
. METHOD is an http method in lowercase. PATH is a relative path on the server (it can be a string, or even a regular expression). HANDLER is a function that Express calls when the route is matched.
Handlers take the form function(req, res) {...}
, where req is the request object, and res is the response object. For example, the handler
function(req, res) {will serve the string 'Response String'. Use the
res.send('Response String');
}
app.get()
method to serve the string Hello Express, to GET requests matching the / root path. Be sure that your code works by looking at the logs, then see the results in your browser, clicking the button ‘Show Live’ in the Glitch UI.
Instructions
Tests
tests:
- text: Your app should serve the string 'Hello Express'
testString: 'getUserInput => $.get(getUserInput(''url'')).then(data => { assert.equal(data, ''Hello Express'', ''Your app does not serve the text "Hello Express"''); }, xhr => { throw new Error(xhr.responseText); })'
Challenge Seed
Solution
// solution required