这是我的App.js文件
const express = require("express");
const dotenv = require("dotenv");
const movieRouter = require("./routes/movieRoutes");
const userRouter = require("./routes/userRoutes");
const app = express();
dotenv.config({ path: "./config.env" });
app.use(express.json());
//Mounting routes
app.use("/kramank-api/v1/movies", movieRouter);
app.use("/kramank-api/v1/users", userRouter);
app.use(express.static("./"));
module.exports = app;
这是我的Server.js文件
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const morgan = require("morgan");
const app = require("./app");
dotenv.config({ path: "./config.env" });
const DB = process.env.DATABASE.replace(
"<password>",
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connection to Database succesful ");
});
const portNumber = 8080;
app.use(morgan("dev"));
app.listen(portNumber, "127.0.0.1", () => {
// eslint-disable-next-line no-console
console.log(`Server is now running on port ${portNumber} ⚡`);
});
问题
当我app.use(morgan("dev"))
在app.js文件中写入它时它可以工作,但是当我将它写入server.js文件时(我在上面粘贴我的代码的方式)它不起作用,甚至没有给出任何错误。