Hey i am runnning an angular2 app with express as backend. Getting the following issue:
[3] Port 4200 is already in use. Use '--port' to specify a different port.
[3] [nodemon] app crashed - waiting for file changes before starting...
[1] [HPM] Error occurred while trying to proxy request /api/jobs from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
server.js:
import * as bodyParser from 'body-parser';
import * as dotenv from 'dotenv';
import * as express from 'express';
import * as morgan from 'morgan';
import * as mongoose from 'mongoose';
import * as path from 'path';
import setRoutes from './routes';
const app = express();
dotenv.load({path: '.env'});
app.set('port', (process.env.PORT || 3000));
app.use('/', express.static(path.join(__dirname, '../')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(morgan('dev'));
mongoose.connect(process.env.MONGODB_URI);
const db = mongoose.connection;
(<any>mongoose).Promise = global.Promise;
db.on('error', console.error.bind(console, 'connection error:'));
db.on('open', () => {
console.log('Connected to MongoDB');
setRoutes(app);
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, '../index.html'));
});
app.listen(app.get('port'), () => {
console.log('Angular 2 Full Stack listening on port ' +
app.get('port'));
});
});
export {app};
edit:
Here the hole code. I think i dont reach the line with db.on('open')
when i inspect the port:
I using concurrently: with this options
"dev": "concurrently \"mongod\" \"ng serve -pc proxy.conf.json --open\" \"tsc -w -p server\" \"nodemon dist/server/server.js\"",
Looks like there is some process running locally on 4200. Here is what I would do-
ps -ef | grep 4200
on your terminal to check which process is running locally kill -9 ${Process_Id}
This will force kill the process.db.on('open')
webpack: Compiled successfully. ^C [1]+ Killed: 9 ng serve
webpack: Compiled successfully. ^Z [1]+ Stopped ng serve
If you use Ctrl+Z in Mac you will have ´Port 4200 is already in use. Use '--port' to specify a different port.´later.
That means the port have running some other process so you have two solution.
1 run the angular in different port by
ng serve --port '{new port}'
2 kill the same port and serve it in same port
sudo kill $(sudo lsof -t -i:4200)
ng serve
For Windows it might be poosible that the port is already running in that case go to cmd as administrator and look up PID for the running port. For example my running port is 4200.
Then run this command on cmd
netstat -a -n -o
Find port with port number 4200 by lookingup in the list or you can right click on terminal and click find here enter 4200 in "find what" and click "find next": For example you found that port number 4200 is used by pid 17200 then type below command in cmd:
taskkill -f /pid 17200
and then you can link your port or start serve command as required
Use npx kill-port 4200
then use ng serve
Reference: “Port 4200 is already in use”. Killing all the processes associated with 4200 did not work.