process.on('unhandledRejection', (err, promise) => {
console.log(`Error: ${err.message}`.red);
//close server & exit process
let server;
server.close(() => process.exit(1));
});
C:\Users\Ford\Desktop\DevCamperAPI\server.js:48
server.close(() => process.exit());
^
TypeError: Cannot read properties of undefined (reading 'close')
can someone help me with this?
I d like to know why i have this error, can someone help me with this? nodejs
Your declaration let server
is without assigning any value, server is undefined. So when you call close
on undefined object you get this error.
The server variable is undefined in this case thus code execution fails when you try to call a method on an undefined variable. There are various ways to create a server using different node modules like https, http, hapi etc. If you have already defined server variable in your module outside this function then you can use that as well to close the server. One of the way to do it is:
var http = require('http');
var server = http.createServer(function (req, res) {
res.write('Application starting');
res.end();
});
server.listen(8080);
process.on('unhandledRejection', (err, promise) => {
server.close(() => process.exit(1));
});
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.