w3 Flashcards
What is Express?
Express is a web application framework for Node.js.
provides all of the tools and basic building blocks you need to get a web server up and running by writing very little code.
allowing us to focus on the application’s logic and content.
In express what do we not have to do that in node.js we do?
we don’t need to-
specify the response headers 200,400 or
add a new line at the end of the string because the framework does it for us.
URL routing
app.get(‘/ab?cd’, function (req, res) {
res.send(‘ab?cd’)
})
This route path will match acd and abcd.
b is silent
app.get(‘/ab+cd’, function (req, res) {
res.send(‘ab+cd’)
})
This route path will match abcd, abbcd, abbbcd, and so on
b can be any number of letters
app.get(‘/abcd’, function (req, res) {
res.send(‘abcd’)
})
This route path will match abcd, abxcd, abRANDOMcd, ab123cd, and so on.
app.get(‘/ab(cd)?e’, function (req, res) {
res.send(‘ab(cd)?e’)
})
This route path will match /abe and /abcde.
cd is silent
app.get(/a/, function (req, res) {
res.send(‘/a/’)
})
This route path will match anything with an “a” in it.
app.get(/.fly$/, function (req, res) {
res.send(‘/.fly$/’)
})
This route path will match butterfly and dragonfly, but not butterflyman, dragonflyman, and so on.
Router parameters
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params object, with the name of the route parameter specified in the path as their respective keys.
Query string
To get the list of query string parameters in a route, req.query can be used
Router parameters vs query string
Router parameters- req.param object
Query string- req.query
Router-level middleware use?
For the sake of separation of concerns (SoC) and maintainability,
t is possible to separate the routes from the main js file using Express.Router
In order to accept requests with a path that starts with BI, ends with DX, and zero or more characters in between, we have to use:
app.get(‘/BI*DX, function(res,res){
});
Assume you have this fragment of code
app.get(‘/item’, function (req, res) {
// print out the item id
})
and the client sends this request:
http://localhost:8080/item?id=123
Which statement prints the value of the id to the console?
console.log(req.query.id)
Assume you have this fragment of code:
app.get(‘/item(w2)?warehouse’, function(req, res){
res.send(‘We got a match’);
})
This route path will match:
/itemwarehouse and /itemw2warehouse