我是Nodejs新手,正在研究Express js,现在我正在研究特定路由的“中间件函数”,我想知道“next有什么用”,意味着在验证“next”函数可以做什么之后做 ?如果我们想移动/重定向到另一个函数那么我们该怎么做?什么是“checkAuthentication”?这是我当前的代码
const express = require('express');
const app = express();
// Custom middleware function
const authMiddleware = (req, res, next) => {
// Check if user is authenticated
const isAuthenticated = checkAuthentication(req);
if (isAuthenticated) {
next();
} else {
// User is not authenticated, send an unauthorized response
res.status(401).send('Unauthorized');
}
};
// Middleware function is applied to specific routes
app.get('/protected', authMiddleware, (req, res) => {
res.send('Protected Route');
});
// Route handler
app.get('/', (req, res) => {
res.send('Home Page');
});
// Start the server
app.listen(3000, () => {
console.log('Server is listening on port 3000');
});
Your Answer
1 个回答
接下来是传递给中间件函数的回调函数。您可以在不同的框架中找到它的不同名称,但概念保持不变。
我将尝试通过您的代码本身来解释中间件。
const express = require('express');
const app = express();
function checkAuthentication(req) {
/*
I am considering this as my authentication function.
When the user logged in, server has sent him a token, which now will act as a validator.
*/
if (req.headers.token) {
const user = someFunctionToFetchUser(req.header.token)
return user
} else {
return false
}
}
/*
Now I want a function to be there on each protected api routes
to make user if user has a valid token then only allow them to interact with the data base otherwise throw an error
*/
const authMiddleware = (req, res, next) => {
// Check if user is authenticated
const isAuthenticated = checkAuthentication(req);
if (isAuthenticated) {
// Now you have verified the has valid a token. So allow user
// to reach to the controller or any other middleware, if any.
next();
} else {
// User is not authenticated, send an unauthorized response
res.status(401).send('Unauthorized');
}
};
// Middleware function is applied to specific routes
app.get('/protected', authMiddleware, (req, res) => {
res.send('Protected Route');
});
// You can have any number of middlewares
app.get('/protected', authMiddleware, someOtherMiddleware, (req, res) => {
res.send('Protected Route');
});
// And also you can pass data from middleware to next middleware/controller also by attahching it with request
function newMiddleware(req, res, next) {
req.foo = "bar" //Now you can access this foo variable in next function.
}
Hot Questions
function_exists()无法判定自定义函数
2024-04-29 11:01:01
google 浏览器 手机版显示的怎么实现
2024-04-23 00:22:19
子窗口操作父窗口,输出没反应
2024-04-19 15:37:47
父窗口没有输出
2024-04-18 23:52:34
关于CSS思维导图的课件在哪?
2024-04-16 10:10:18
Hot Tools
vc9-vc14(32+64位)运行库合集(链接在下方)
phpStudy安装所需运行库集合下载
VC9 32位
VC9 32位 phpstudy集成安装环境运行库
php程序员工具箱完整版
程序员工具箱 v1.0 php集成环境
VC11 32位
VC11 32位 phpstudy集成安装环境运行库
SublimeText3汉化版
中文版,非常好用
热门话题
抖音等级价目表1-75
20334
7
20334
7
wifi显示无ip分配
13530
4
13530
4
虚拟手机号接收验证码
11850
4
11850
4
gmail邮箱登陆入口在哪里
8835
17
8835
17
windows安全中心怎么关闭
8420
7
8420
7
热门文章
2025年加密货币市场十大趋势预测:下一个风口在哪里?
2025-11-07
By DDD
币圈土狗项目如何识别?避免归零币的陷阱与风险预警
2025-11-07
By DDD
win10字体安装后在软件里找不到怎么办_win10字体安装与识别方法
2025-11-07
By DDD
高效处理PHP表单中动态数量的问答数据更新
2025-11-07
By DDD
季节的故事:大巴扎 - SOS!逃跑的鸟儿!请求演练
2025-11-07
By DDD





