Express
Express
Express
CheatSheet
In this Cheatsheet, we will cover the basics of Express.js. We will
provide examples to help you understand how Express.js works
and how to use them in your own web development projects.
Whether you are a beginner or an experienced developer, this PDF
can serve as a useful reference guide.
EXPRESS
Express.js is a popular open-source web application framework for
Node.js, a JavaScript runtime that allows developers to build
scalable, high-performance server-side applications. It provides a
wide range of features and utilities for web application
development, such as routing, middleware, and template
rendering.
2|Page
INSTALLATION
BASIC ROUTING:
// GET
app.get('/', function (req, res) {
res.send('Hello World!')
})
// POST
app.post('/', function (req, res) {
res.send('POST request. body:', req.body)
})
// DELETE
app.delete('/:id', function (req, res) {
res.send('DELETE request for id:'. req.params.id)
})
3|Page
STATIC FILE SERVING:
app.use(express.static(__dirname + '/public'));
app._router.stack.forEach(function(r) {
if (r.route && r.route.path) {
console.log(r.route.path)
}
});
4|Page
ADDING ROUTES FROM: /routes/users.js
app.use('/user', require('./routes/user'));
REDIRECTS:
5|Page