英文:
Node.JS one of three same-structured paths is not working
问题
我对Node.js相当新,并且目前正在构建一个项目的后端API。我的应用程序中有3个路由:societies、users和emails。
以下是你的代码中的翻译部分:
// 用于邮件功能的模块
emailsModule = require('./api/routes/emails')(connection);
const emailsRoutes = emailsModule.router;
app.use('/emails', emailsRoutes);
// 用于社团功能的模块
societyModule = require('./api/routes/societies')(connection);
const societyRoutes = societyModule.router;
app.use('/societies', societyRoutes);
// 用于用户功能的模块
usersModule = require('./api/routes/users')(connection);
const usersRoutes = usersModule.router;
app.use('/users', usersRoutes);
"societies
" 和 "emails
" 模块是有效的,但 "emails" 模块不起作用。我只是无法弄清楚为什么它不起作用,我尝试了各种方法,但邮件模块就是不起作用。Postman输出显示 "无法获取 /emails/。
这是我的 module.exports
函数:
module.exports = function(connection) {
module.connection = connection;
sql = connection;
module.router = router;
console.log("sacramento"); // 测试输出
return module;
}
一个示例路由如下:
router.get("/:email"), (req, res) => {
var credentialCheck = new sql.Request();
credentialCheck.input("email", sql.VarChar, req.query.email);
credentialCheck.input("passwort", sql.VarChar, req.body.password);
credentialCheck.execute("checkSocietyCredentials",
(er, result) => {
if (er) {
res.status(200).send({
error: er
});
}
else {
res.status(200).send(
{
isEmailValid : result.recordsets[0][0],
isPasswordValid : result.recordsets[0][1]
}
);
}
});
}
可能是一个小错误,比如路径中的 "//"。我使用的此模块的URL是 "http://localhost:3000/emails/?email=example@gmail.com"。如果有人能找到我的脚本中的错误,我将非常感激。
英文:
I am pretty new to Node.js and I am currently building a backend api for a project. I´ve got 3 routes in my app. societies, users and emails.
//module for email stuff
emailsModule = require('./api/routes/emails')(connection);
const emailsRoutes = emailsModule.router;
app.use('/emails', emailsRoutes);
//module for societies
societyModule = require('./api/routes/societies')(connection);
const societyRoutes = societyModule.router;
app.use('/societies', societyRoutes);
//module for users
usersModule = require('./api/routes/users')(connection);
const usersRoutes = usersModule.router;
app.use('/users', usersRoutes);
The societies
and emails
modules are working, but the emails module is not working.
I just cannot figure out why it isn´t working, I have tried various things but the emails module just doesn´t work. Postman output says "Cannot GET /emails/.
This is my module.exports function:
module.exports = function(connection) {
module.connection = connection;
sql = connection;
module.router = router;
console.log("sacramento"); //test output
return module;
}
An example route is
router.get("/:email"), (req, res) => {
var credentialCheck = new sql.Request();
credentialCheck.input("email", sql.VarChar, req.query.email);
credentialCheck.input("passwort", sql.VarChar, req.body.password);
credentialCheck.execute("checkSocietyCredentials",
(er, result) => {
if ( er ) {
res.status(200).send({
error:er
});
}
else {
res.status(200).send(
{
isEmailValid : result.recordsets[0][0],
isPasswordValid : result.recordsets[0][1]
}
);
}
});
}
It could be just a little stupidity error like // in a path.
The URL i am using for this module is http://localhost:3000/emails/?email=example@gmail.com
. I I would be really thankful if someone would find the error in my script.
答案1
得分: 0
我看到了多个需要更正的问题。
这个语法是错误的:
router.get("/:email"), (req, res) => {
应该是:
router.get("/:email", (req, res) => {
然后你需要在函数代码的末尾添加 )
。
你实际上只是在定义一个路由:
router.get("/:email")
完全没有路由处理程序。然后,你声明了一个箭头函数,但没有对其执行任何操作。假设这实际上在解释器中解析了,你只是不走运,因为一个拼写错误损害了你的代码,却恰好能够编译而且没有报错(在所有语言中都会偶尔发生,非类型化语言中更常见)。
此外,对于以下部分:
app.use('/emails', emailsRoutes)
以及
router.get("/:email")
你请求的路由应该是 /emails/someEmail
,其中 someEmail
是你想要的 :email
参数。如果这是一个实际的电子邮件地址,它必须进行正确的 URL 编码。
另外,在 /:email
路由处理程序中,你正在引用 req.body.password
。这将不起作用。req.body
对于 GET 请求是空的。它仅用于 POST 或 PUT 请求,并且只有在你使用正确的中间件来读取和解析请求体到 req.body
时才会被填充。
如果你打算将电子邮件和密码都传递到这个路由中,那么它应该可能是一个 POST 请求,并且电子邮件和密码都应该出现在请求体中。
英文:
I see multiple issues that will need to be corrected.
This syntax is wrong:
router.get("/:email"), (req, res) => {
It should be
router.get("/:email", (req, res) => {
and then you will have to add )
at the end of the function's code.
You were essentially just defining a route for:
router.get("/:email")
with no route handler at all. And, then you were declaring an arrow function, but not doing anything with it. Assuming this actually parsed in the interpreter, you just got unlucky in that a typo that messed up your code just happened to compile and not report an error (happens sometimes in all languages, more often in a non-typed language).
Also, with:
app.use('/emails', emailsRoutes)
and
router.get("/:email")
The route you request should be /emails/someEmail
where someEmail
is whatever you want the :email
parameter to be. If that's an actual email address, it will have to be properly URL encoded.
And, in that /:email
route handler, you are referring to req.body.password
. That will not work. req.body
is empty for a GET request. That is only used for a POST or PUT request and will only be filled in if you use the right middleware o read and parse the body into req.body
.
If you are intending to pass both an email and a password to this route, then it should probably be a POST request and both email and password should arrive in the body.
答案2
得分: 0
这是因为您将电子邮件路由参数定义为/emails
的子路由。
因此,预期的URL应该类似于这样:http://localhost:3000/emails/example@gmail.com
。
您有两个选项。
- 将路由定义更改为
router.get("/")
- 保留您的路由定义为
router.get("/:email")
,更改URL为上面的示例,并像这样访问电子邮件req.params.email
我猜您正在使用express。有关路由参数的更多信息,请查看此处的路由文档。
英文:
This is because you defined an email route parameter as a sub route of /emails
.
Therefore the expected url should be something like this http://localhost:3000/emails/example@gmail.com
.
You have two options.
- change the route definition to
router.get("/", ...)
- leave your route definition as
router.get("/:email", ...)
, change the url to be like the example above and access the email like soreq.params.email
I guess you are using express. Check the routing documentation here for more information regarding route parameters.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论