如何运行 ExpressJS 应用程序

时间:2021-05-09 15:35:17

标签: javascript node.js express mongoose mongodb-atlas

除了我的代码中的“抛出新错误”外,似乎没有任何工作,我确定已经安装了所有必要的软件包。我在代码的其他地方没有收到任何错误。我似乎无法找到问题所在。希望有人知道如何解决这个问题。

const express = require('express');         // we're making an ExpressJS App
const bodyParser = require('body-parser');  // we'll use body-parser extensively
//const port =3000
const app = express();                      // create the ExpressJS app

// parse the different kinds of requests (content-type) the app handles
// use the "use" method to set up the body-parser middlewear
app.use(express.json())                          //  application/json
app.use(express.urlencoded({ extended: true }))  // application/x-www-form-urlencoded


// Set up Mongoose and our Database connection
const dbConnect = require('./config/AtlasConnect.js');
const mongoose = require('mongoose');

// Set up connection to the database
mongoose.connect(dbConnect.database.url, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(() => {
    console.log("Successfully connected to the MongoDB database");    
}).catch(err => {
    console.log('Unable to connect to the MongoDB database', err);
    process.exit();
});

throw new Error("NOT WORKING!");


// // create our test route (reply by sending a JSON message as response)
// app.get('/', (req, res) => {
//     res.json({"message": "My Phone Shop App. Use the app to manage your favourite s!"});
// });
require('./app/routes/Users.routes.js')(app);

// listen for requests on port 3000
app.listen(3000, () => {
    console.log("Server listening on port 3000");
});

这是我在终端中得到的错误:

PS C:\Users\jaffe\Documents\assignment-06-17518623> node assignment-06.js
C:\Users\jaffe\Documents\assignment-06-17518623\assignment-06.js:44
throw new Error("NOT WORKING!");
^

Error: NOT WORKING!
    at Object.<anonymous> (C:\Users\jaffe\Documents\assignment-06-17518623\assignment-06.js:44:7)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

1 个答案:

答案 0 :(得分:0)

throw new Error("NOT WORKING!"); 总是抛出异常,并停止代码执行。
请参阅文档 https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Statements/throw