Sept 20, 2022
Express is a server-side web framework that provides various features to assist in creating web applications.
Handlebars is templating language that provides additional functionality beyond static HTML files, including placeholder expressions and control flow helpers.
bin/
Contains the startup script; typically not necessary to modify
public/
Static assets like stylesheets, images, and client-side JavaScript files
routes/
Router files that contain router definitions that detail the actions that happen when defined paths are accessed
views/
Handlebars files that the routers will use to render HTML
app.js
Application setup, used by the startup script
package.json
Lists dependencies required by the application, as well as additional configuration
A route definition is made up of an HTTP method, a path, and a handler function. When a path is accessed using the specified HTTP method, the handler function will execute.
router.get('/', (req, res) => {
// additional code here
res.render('index', { title: 'Home Page' });
});
router.post('/submit', (req, res) => {
// additional code here
res.render('submit');
});
The req
argument is the request object
and contains information about the HTTP request.
The res
argument is the response object
and is used in setting up and handling the response.
req.query
Contains query string variables; given path
/collection?sortBy=name&order=asc
req.query.sortBy
will have the value of
name req.query.order
will have the value of
asc
req.body
Contains post body variables
<form method="post">
<input type="text" name="firstName">
<button type="submit">Submit</button>
</form>
req.body.firstName
will have the value
of what was inputted into the text box
req.params
Contains values of route parameters. The route path definition can contain placeholders by prepending with a :
router.get('/users/:user', ...);
Give the path /users/kidddw
, then
req.params.user
would contain
kidddw
.
res.render
res.render(viewString, localVariablesObject)
Responds with the rendered named view with the properties of localVariablesObject passed into the view
res.send
res.send(textString)
Responds with the raw text
res.json
res.json(variable)
Sends the JSON representation of the JavaScript variable