I cant understand why I see this error.
my app.js
file
const express = require("express");
const morgan = require('morgan');
const app = express();
const tourRouter = require('./routers/tourRouter');
const userRouter = require('./routers/userRouter');
app.use(express.json());
app.use(morgan('dev'));
app.use((req,res,next)=>{
req.RequestTime = new Date().toISOString();
next();
});
app.use('/api/v1/tours',tourRouter);
app.use('/api/v1/users',userRouter);
const port = 3000;
app.listen(port, () => {
console.log(`App running on port ${port}`);
});
this runs without a problem, but when I cut
const port = 3000;
app.listen(port, () => {
console.log(`App running on port ${port}`);
});
this part and I write app.js folder exports.module = app;
and I create server.js
file
const app = require('./app');
const port = 3000;
app.listen(port, () => {
console.log(`App running on port ${port}`);
});
this doesn't work and I get
TypeError: app.listen is not a function
As @Dickens A S commented, you need to assign the object: app: instance of express
to the module.exports
in the file: "app.js"
.
Read Doc: Node.js: module.exports
many want their module to be an instance of some class. To do this, assign the desired export object to module.exports.
Add this in the of of file:app.js
:
module.exports = app;