[英]TypeError: res.sendStatus is not a function - Why do I keep getting this error?
I have been working in Node.js/Express in making a REST Api for the past several months.在过去的几个月里,我一直在使用 Node.js/Express 制作 REST Api。 I'm running into this issue with my async function in my controller.js file.
我的 controller.js 文件中的异步函数遇到了这个问题。 My callback function receives my
request
from the client but when I want to send a response
I keep getting this TypeError: res.sendStatus is not a function
Error.我的回调函数从客户端接收我的
request
,但是当我想发送response
我不断收到此TypeError: res.sendStatus is not a function
Error。
Here's my code:这是我的代码:
index.js
const express = require('express');
const { parseData } = require("../controllers/parse.controller");
const { checkFirebaseLogin } = require("../controllers/checkFirebaseLogin.controller");
var bodyParser = require('body-parser');
var router = express.Router();
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: false }));
router.post('/api/idToken', checkFirebaseLogin);
router.post('/', parseData);
module.exports = router
Here is my Controller.js file这是我的 Controller.js 文件
checkFirebaseLogin.controller.js
const { checkUserLogin } = require("../services/checkUserLogin.service");
const checkFirebaseLogin = async (req, res) => {
console.log("Checking on user token ...");
const userToken = req.headers;
try {
var userUID = await checkUserLogin(userToken);
console.log("Here's the repsonse:");
console.log(userUID)
res.sendStatus(201)
} catch (error) {
console.log("I'm here right before the error message on checking the user token")
//console.log(error.message);
console.log(error)
}
};
module.exports = {
checkFirebaseLogin
};
I am able to get the req.headers
but when I try to send a response (via res.sendStatus(200)
) back to the client I always get an error.我能够获得
req.headers
但是当我尝试将响应(通过res.sendStatus(200)
)发送回客户端时,我总是收到错误消息。 Where I am going wrong?我哪里出错了?
Updated: Jan 12 Also, is here is my app.js so that you can see how I'm dealing with my routes.更新:1 月 12 日此外,这里是我的 app.js,以便您可以看到我如何处理我的路线。 The client (based on my code would go from the
app.js -> routes/index.js -> controllers/checkFirebaseLogin.controller.js
)客户端(基于我的代码将来自
app.js -> routes/index.js -> controllers/checkFirebaseLogin.controller.js
)
app.js
var express = require('express');
var http = require("http");
var app = express();
const routes = require('./routers');
//app.get('/', (req, res) => res.send('App is working'));
app.use('/', routes);
app.use('/api/idToken', routes);
http.createServer(routes).listen(5000, () => {
console.log("server up");
});
module.exports = {
app
};
It does appear that you aren't setting up your http server and Express app
object correctly.您似乎没有正确设置 http 服务器和 Express
app
对象。 You're trying to pass a router
to a plain http server - that's not going to work.您正在尝试将
router
传递给普通的 http 服务器 - 这是行不通的。 You need to pass a router to an app.use()
.您需要将路由器传递给
app.use()
。
Change from this:从此改变:
var express = require('express');
var http = require("http");
var app = express();
const routes = require('./routers');
//app.get('/', (req, res) => res.send('App is working'));
app.use('/', routes);
app.use('/api/idToken', routes);
http.createServer(routes).listen(5000, () => {
console.log("server up");
});
module.exports = {
app
};
to this:对此:
var express = require('express');
var app = express();
const routes = require('./routers');
//app.get('/', (req, res) => res.send('App is working'));
app.use('/', routes);
app.use('/api/idToken', routes);
app.listen(5000, () => {
console.log("server up");
});
module.exports = {
app
};
The key here is that the app
object, NOT the router
object needs to be passed to the http server.这里的关键是
app
对象,而不是router
对象需要传递给 http 服务器。 In this modified code, we use app.listen()
to create the server which does that for us automatically.在此修改后的代码中,我们使用
app.listen()
创建自动为我们执行此操作的服务器。 In your original code, you could have done:在您的原始代码中,您可以这样做:
http.createServer(app).listen(...)
Note passing app
, instead of router
.注意传递
app
,而不是router
。 But, I prefer to just use app.listen()
which is designed as a coding shortcut for this (it creates the http server object for you).但是,我更喜欢只使用
app.listen()
,它被设计为一个编码快捷方式(它为你创建了 http 服务器对象)。
Also, this looks suspicious:此外,这看起来很可疑:
app.use('/', routes);
app.use('/api/idToken', routes);
Because you're making the exact same functionality be present at two different URL locations (unless that's what you oddly intended).因为您要在两个不同的 URL 位置提供完全相同的功能(除非这是您奇怪的意图)。 You can technically do this, but it's very unusual so I'm wondering if that's really what you intended.
从技术上讲,您可以这样做,但这是非常不寻常的,所以我想知道这是否真的是您的意图。
You did not initialize express properly.您没有正确初始化 express。
var app = express();
...
var router = express.Router();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
router.post('/api/idToken', checkFirebaseLogin);
router.post('/', parseData);
...
app.use(router);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.