I have a code like this. And it is a code you can find in a numerous docs and tutorials.
<code>const express = require('express');
const app = express();
// Authentication middleware
app.use((req, res, next) => {
// Simulate a DB check for authentication
console.log('Checking authentication...');
next();
});
// Route handlers
app.get('/example', (req, res) => {
res.send('This is an example route.');
});
// 404 handler should be the last middleware
app.use((req, res, next) => {
res.status(404).send('Route not found');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
</code>
<code>const express = require('express');
const app = express();
// Authentication middleware
app.use((req, res, next) => {
// Simulate a DB check for authentication
console.log('Checking authentication...');
next();
});
// Route handlers
app.get('/example', (req, res) => {
res.send('This is an example route.');
});
// 404 handler should be the last middleware
app.use((req, res, next) => {
res.status(404).send('Route not found');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
</code>
const express = require('express');
const app = express();
// Authentication middleware
app.use((req, res, next) => {
// Simulate a DB check for authentication
console.log('Checking authentication...');
next();
});
// Route handlers
app.get('/example', (req, res) => {
res.send('This is an example route.');
});
// 404 handler should be the last middleware
app.use((req, res, next) => {
res.status(404).send('Route not found');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
Potential issue here. If a page not exist we still check auth.
Can we somehow avoid this, and check if a route exist first.
You can chain middleware functions in Express. So, your authentication middleware can be added to routes for which you need to do verify the credentials.
It would look something like:
<code>const express = require('express');
const app = express();
// Authentication middleware
const auth = (req, res, next) => {
// Simulate a DB check for authentication
console.log('Checking authentication...');
next();
});
// Route handlers
// Requires authentication
app.get('/example', auth, (req, res) => {
res.send('This is an example route.');
});
// Does not requires authentication
app.get('/example-no-auth', (req, res) => {
res.send('This is an example route.');
});
// 404 handler should be the last middleware
app.use((req, res, next) => {
res.status(404).send('Route not found');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
</code>
<code>const express = require('express');
const app = express();
// Authentication middleware
const auth = (req, res, next) => {
// Simulate a DB check for authentication
console.log('Checking authentication...');
next();
});
// Route handlers
// Requires authentication
app.get('/example', auth, (req, res) => {
res.send('This is an example route.');
});
// Does not requires authentication
app.get('/example-no-auth', (req, res) => {
res.send('This is an example route.');
});
// 404 handler should be the last middleware
app.use((req, res, next) => {
res.status(404).send('Route not found');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
</code>
const express = require('express');
const app = express();
// Authentication middleware
const auth = (req, res, next) => {
// Simulate a DB check for authentication
console.log('Checking authentication...');
next();
});
// Route handlers
// Requires authentication
app.get('/example', auth, (req, res) => {
res.send('This is an example route.');
});
// Does not requires authentication
app.get('/example-no-auth', (req, res) => {
res.send('This is an example route.');
});
// 404 handler should be the last middleware
app.use((req, res, next) => {
res.status(404).send('Route not found');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
So now, when the server receives a request:
- It will try to match the defined routes
- If it exists, it will execute the middleware (if provided), and then the function will return the response.
- If it does not find a matching path, it will return a 404
You can chain multiple middleware functions like this.
More info: express middleware