Example 1: urlencoded json express
var express = require('express')
var app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.post('/profile', function (req, res, next) {
console.log(req.body)
res.json(req.body)
})
Example 2: app.get
const express = require('express');
const app = express();
const { getElementById, getIndexById, updateElement,
seedElements, createElement } = require('./utils');
const PORT = process.env.PORT || 4001;
app.use(express.static('public'));
const expressions = [];
seedElements(expressions, 'expressions');
const animals = [];
seedElements(animals, 'animals');
app.get('/expressions', (req, res, next) => {
res.send(expressions);
});
app.get('/expressions/:id', (req, res, next) => {
const foundExpression = getElementById(req.params.id, expressions);
if (foundExpression) {
res.send(foundExpression);
} else {
res.status(404).send();
}
});
app.put('/expressions/:id', (req, res, next) => {
const expressionIndex = getIndexById(req.params.id, expressions);
if (expressionIndex !== -1) {
updateElement(req.params.id, req.query, expressions);
res.send(expressions[expressionIndex]);
} else {
res.status(404).send();
}
});
app.post('/expressions', (req, res, next) => {
const receivedExpression = createElement('expressions', req.query);
if (receivedExpression) {
expressions.push(receivedExpression);
res.status(201).send(receivedExpression);
} else {
res.status(400).send();
}
});
app.delete('/expressions/:id', (req, res, next) => {
const expressionIndex = getIndexById(req.params.id, expressions);
if (expressionIndex !== -1) {
expressions.splice(expressionIndex, 1);
res.status(204).send();
} else {
res.status(404).send();
}
});
app.get('/animals', (req, res, next) => {
res.send(animals);
});
app.get('/animals/:id', (req, res, next) => {
const animal = getElementById(req.params.id, animals);
if (animal) {
res.send(animal);
} else {
res.status(404).send();
}
});
app.post('/animals', (req, res, next) => {
const receivedAnimal = createElement('animals', req.query);
if (receivedAnimal) {
animals.push(receivedAnimal);
res.status(201).send(receivedAnimal);
} else {
res.status(400).send();
}
});
app.put('/animals/:id', (req, res, next) => {
const animalIndex = getIndexById(req.params.id, animals);
if (animalIndex !== -1) {
updateElement(req.params.id, req.query, animals);
res.send(animals[animalIndex]);
} else {
res.status(404).send();
}
});
app.delete('/animals/:id', (req, res, next) => {
const animalIndex = getIndexById(req.params.id, animals);
if (animalIndex !== -1) {
animals.splice(animalIndex, 1);
res.status(204).send();
} else {
res.status(404).send();
}
});
app.listen(PORT, () => {
console.log(`Server is listening on ${PORT}`);
});
Example 3: what is middleware in node js
Notice the call above to next(). Calling this function invokes the next middleware function in the app. The next() function is not a part of the Node.js or Express API, but is the third argument that is passed to the middleware function. The next() function could be named anything, but by convention it is always named “next”. To avoid confusion, always use this convention.
Example 4: express router middleware
var express = require('express')
var app = express()
var router = express.Router()
router.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})
router.use('/user/:id', function (req, res, next) {
console.log('Request URL:', req.originalUrl)
next()
}, function (req, res, next) {
console.log('Request Type:', req.method)
next()
})
router.get('/user/:id', function (req, res, next) {
if (req.params.id === '0') next('route')
else next()
}, function (req, res, next) {
res.render('regular')
})
router.get('/user/:id', function (req, res, next) {
console.log(req.params.id)
res.render('special')
})
app.use('/', router)