Created
May 23, 2019 15:15
-
-
Save brendaMan/0b6aad0140c0f56e656b9c0135a2174d to your computer and use it in GitHub Desktop.
Express 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const server = express(); | |
const port = 3000; | |
// A route answering the url /api/movies which sends as information a string containing "All films" | |
server.get('/api/movies', function(req, res) { | |
res.end('All films'); | |
}); | |
// A route that responds to the url /api/movies/\<movie id\> that sends as information a JSON object containing {id: \<movie id\>} | |
server.get('/api/movies/:movieid', function(req, res) { | |
res.json({params: req.params, query: req.query }); | |
}); | |
// A route responding to the /api/employee/ url that sends a status 304 | |
server.get('/api/employee/', function(req, res, next) { | |
res.status(304); | |
if (req.query.name) next() // | |
else res.end(); | |
}); | |
// A route /api/employee?name=\<employee name\> that returns a 404 status and a string containing "Unable to retrieve employee" <name of employee the employee \> " | |
server.get('/api/employee', function(req, res) { | |
res.status(404); | |
res.send('Unable to retrieve employee ' + req.query.name); | |
}); | |
server.listen(port, function() { | |
console.log('Listening on port ' + port); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment