-1

I am trying to run my node.js application with 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 /

I tried running nodemon nodemon ./app.js and nodemon start 9000 but the error is still the same.

Here's my 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);
Alku
  • 1
  • 2

2 Answers2

2

Have you try to use this?

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

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');
})
saidutt
  • 289
  • 1
  • 7