简体   繁体   English

这个 router.get("/".......) 函数是如何工作的?

[英]How does this router.get("/".......) function work?

I'm new to express and ES6 in general.我是新来的表达和一般的 ES6。 I dont understand the notation of what exactly is going on in the line where we have router.get(. We have 'auth' which is being passed as a parameter but what exactly is happening and how does router.get know how to handle it? At a glance it I've created a file named auth.js which has been imported as auth and that is the object is what we are talking about (please correct me if my terminology is wrong). Or is auth a function and we've passed the function into get and it will run this auth function when router.get is called?我不明白在我们有 router.get( 的行中到底发生了什么。我们有 'auth' 作为参数传递但到底发生了什么以及 router.get 如何知道如何处理它? 乍一看,我创建了一个名为 auth.js 的文件,它已作为 auth 导入,这就是我们正在谈论的对象(如果我的术语有误,请纠正我)。或者 auth 是一个函数,我们'已经将函数传递给 get 并且当 router.get 被调用时它会运行这个 auth 函数?

const router = express.Router();
const auth = require("../../middleware/auth");

const User = require("../../models/User");
// @route    GET api/profile
// @desc     Test route
// @access   Public

router.get("/", auth, async (req, res) => {
    try {
        // we are omitting the password
        const user = await User.findById(req.user.id).select("-password");
        res.json(user);
    } catch (err) {
        console.error(err.message);
        res.status(500).send("Server Error");
    }
});

module.exports = router;```

auth is a middleware function that gets to preprocess the request before the handler receives it. auth是一个中间件函数,它在处理程序接收请求之前对请求进行预处理。 the auth function is most likely attaching the user/id to the request, which the handler then uses to query for the actual User object. auth 函数很可能将用户/id 附加到请求中,然后处理程序使用该请求来查询实际的用户对象。

Without seeing the auth implementation I can't say exactly what it's doing, (and it's been a while since i've worked in express) but just as an example it could be looking at cookies or request headers to get the user id and attach it to the request object so all subsequent handlers have easy access to it.在没有看到 auth 实现的情况下,我无法准确地说出它在做什么,(自从我从事 express 工作以来已经有一段时间了),但作为一个例子,它可能正在查看 cookie 或请求标头以获取用户 ID 并附加它到请求对象,因此所有后续处理程序都可以轻松访问它。

If you look at the documentation for router.METHOD you'll see that it takes as many callbacks as you want.如果您查看router.METHOD 的文档,您会发现它需要尽可能多的回调。 Each function can do whatever it needs to do and then call next() to pass processing to the next handler, or it can handle the request itself, responding and/or stopping further processing.每个函数都可以做它需要做的任何事情,然后调用 next() 将处理传递给下一个处理程序,或者它可以自己处理请求,响应和/或停止进一步的处理。

The auth which you have written is a Router-level middleware.您编写的auth是路由器级别的中间件。 It will call before the function execute inside the route.它将在函数在路由内执行之前调用。

If your authentication failed then you will get bad response/error response.如果您的身份验证失败,那么您将收到错误的响应/错误响应。 If your authentication passes then only you will allow to request.如果您的身份验证通过,那么只有您允许请求。

For more info see express official docs https://expressjs.com/en/guide/using-middleware.html#middleware.router有关更多信息,请参阅 express 官方文档https://expressjs.com/en/guide/using-middleware.html#middleware.router

auth 是一个中间件函数,如果您尝试访问的路由没有通过 auth 中间件,您将收到错误消息。

Found out that in the other versions of node, the error of "route.get is not a function" can be resolved by using app.get , app.post or whichever API route you where creating.发现在其他版本的 node 中, "route.get is not a function"的错误可以通过使用app.getapp.post或您创建的任何API路由来解决。 This resolve needs you to remove the declaration line var router= express.Router() completely and have app = express; //(assuming you assigned imported app=require('express');此解决方案需要您完全删除声明行var router= express.Router()并拥有app = express; //(assuming you assigned imported app=require('express'); app = express; //(assuming you assigned imported app=require('express');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM