简体   繁体   中英

How to connect to node port from angular 2 project

js file in folder A which handles my routes and connect to database and it looks like this

var express = require('express');        // call express
var app = express();                 // define our app using express
var bodyParser = require('body-parser');

var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/ExpenseUsers");

var User = require('./Models/Users');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 5555;        // set our port

var router = express.Router();              // get an instance of the      express Router

router.use(function (req, res, next) {
next();
});

router.route('')
.post(function (req, response) {

    var user = new User(req.body);

    user.save(function (err, resource) {
        if (err) {
            response.send(err).status(501);
        } else {
            response.json(resource).status(201);
        }
    });

})

.get(function (req, res) {
    User.find(function (err, users) {
        if (err)
            res.send(err);

        res.json(users);
    });
})


router.get('/', function (req, res) {
  res.json({ message: 'hooray! welcome to my api!' });
});

app.use('/User', router);

app.listen(port);

I tried creating user using Postman tool and it worked fine.Then i cloned a angular2 project in some other folder when i run the angular2 it opened on port 5555.Then i got this error in server.js saying

Error: listen EADDRINUSE :::5555

Can someone please tell me what is wrong here.

It's very clear error message and it says port 5555 is used by another process.

You must change Angular's or web server's port (I recommend changing Angular's port because if you change web server's port you must change its' references on Angular code as well).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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