简体   繁体   English

node.js应用程序中的GET /错误是什么?

[英]What is GET/ error in node.js application?

I am trying to run my node.js application with npm start 我正在尝试使用npm start运行我的node.js应用npm start

The output I am getting is: 我得到的输出是:

$ npm start

> alaka@1.0.0 start C:\Users\Alaka\Downloads\A4\API assignment 4
> nodemon app.js

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js` 

When I try to connect via localhost, I get error Cannot GET / 当我尝试通过localhost连接时,出现错误Cannot GET /

I tried running nodemon nodemon ./app.js and nodemon start 9000 but the error is still the same. 我尝试运行nodemon nodemon ./app.jsnodemon start 9000但是错误仍然相同。

Here's my app.js 这是我的app.js

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

var uploadRouter = require('./Upload/upload');
const app = express();
app.use(express.json());

app.use('/uploads',express.static('./public/uploads'));
app.use(express.urlencoded({ extended: false }));

require('./DB/dbmongoose');
const User = require('./model/user');
const Product= require('./model/product');

app.use(bodyParser.urlencoded({ extended: false }));
app.use('/upload', uploadRouter);
module.exports = app;
app.post('/Register', (req, res) => {

    var user = new User(req.body);
    console.log(req.body);
    user.save();
});

app.get('/Login', function (req, res) {
    User.find().then(function (user) {
        res.send(user);
    }).catch(function (e) {
        res.send(e)
    });

});

app.post('/addproduct', (req, res) => {

    var product = new Product(req.body);
    console.log(req.body);
    product.save().then(function(product){
            res.send(product);
    }).catch(function (e){
        res.send(e);
    });
    });

app.get('/allproduct',(req, res, next) => {
    console.log('ok');
    Product.find({})
        .then((product) => {
            res.json(product);
        }, (err) => next(err))
        .catch((err) => next(err));
});

app.listen(9000);

Have you try to use this? 你试过用这个吗?

app.get('/', function (req, res) {
  throw new Error('BROKEN') // Express will catch this on its own.
})

Try using the following middleware for connection 尝试使用以下中间件进行连接

//set port for connection
app.set('port', (process.env.PORT || 9000));

app.listen(app.get('port'), function(){
    console.log('Server started on port '+app.get('port'));
});

Also as mentioned in the other comment. 也如其他评论中所述。 Please have a route for home/* 请提供回家路线//

app.get('/', (req, res) => {
  throw new Error('BROKEN');
})

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

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