简体   繁体   English

如何运行 ExpressJS 应用程序

[英]how to run an ExpressJS application

Nothing seems to work except the "throw new Error" in my code, I have installed all the necessary packages I'm sure.除了我的代码中的“抛出新错误”之外,似乎没有任何效果,我确定我已经安装了所有必要的软件包。 I am getting no errors in other places of my code.我的代码的其他地方没有错误。 I can't seem to find the problem.我似乎找不到问题所在。 Hoping someone knows how to solve this.希望有人知道如何解决这个问题。

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");
});

This is the error I get in terminal:这是我在终端中遇到的错误:

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

Line throw new Error("NOT WORKING;");throw new Error("NOT WORKING;"); always throws exception, and stops code execution.总是抛出异常,并停止代码执行。
See documentation https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Statements/throw请参阅文档https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Statements/throw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM