I have been working in Node.js/Express in making a REST Api for the past several months. I'm running into this issue with my async function in my controller.js file. 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.
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
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. 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. The client (based on my code would go from the 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. You're trying to pass a router
to a plain http server - that's not going to work. You need to pass a router to an 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. In this modified code, we use app.listen()
to create the server which does that for us automatically. In your original code, you could have done:
http.createServer(app).listen(...)
Note passing app
, instead of 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).
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). 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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.