node.js express socket.io port 3000 in use - node.js

I've been following this(http://socket.io/get-started/chat/) tutorial on how to make a simple chat application using socket.io.
I tried to however use Express to create it and I was wondering why port 3000 is already in use? The code below will not work unless I change the port number.
/* Make the http server listen on port 3000. */
http.listen(3000, function(){
console.log('listening on *:3000');
});
Does express use the port to do other things like routing or something?
Is there a simple way to find what is happening on that port?
I may also be doing something dodgy with my require things:
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var router = express.Router();
var io = require('socket.io')(http);
Thanks.

I ran into this problem too and I solved it by this:
Do not use npm start to start your web app
Use node app.js instead

Try running:
netstat -anp tcp | grep 3000
This should show you the name of the process that is using port 3000. Here's another issue on StackOverflow that covers this issue in more depth.

One of the best way to do this during the development would be through IDE where you can do comprehensive debugging and step through the code.
If you are using WebStorm, this works.
From run configurations -> Edit Configurations -> Nods.js and add the app.js as the node parameter. See below arrow in the screenshots for more details.

I resolved the same problem with an express app doing this:
Edit the file "yourap/bin/www"
find the line :
var port = normalizePort(process.env.PORT || '3000');
replace it by:
var port = normalizePort('XXXX');
where XXXX is the port number you want to use
Then youre free to do npm start! xD

I had (forgotten that I had) previously installed ntop, which by default also uses port 3000, and was therefore getting the same error as described here.
As others have mentioned, use netstat or lsof to find the offending service (and prefix the command with sudo, to get the correct process name):
sudo lsof -P | grep ':3000'
- or -
sudo netstat -anp tcp | grep 3000
On Ubuntu, the service is disabled with (simply):
service ntop stop

Similar to answer above to not use npm start.
I was using nodemon and with expressjs and expressjs generator. I was using nodemon to execute npm start, while npm start itself execute node ./NodeApp/bin/www
So i edited to make nodemon to execute node ./NodeApp/bin/www by itself and that error go away.
Conclusion
Before
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./NodeApp/bin/www",
"build": "webpack --watch",
"dev": "nodemon --exec npm start"
},
After
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --watch",
"dev": "nodemon --exec node ./NodeApp/bin/www"
},
So now I run my sever with npm run dev and no more errors.

for me helps to make use 3000 || 3333, and it's fix the issue

I solved it by this:
npm install shelljs
and add code for kill nodejs process before start listen port
var shell = require('shelljs');
shell.exec("pkill nodejs");
shell.exec("pkill node");
/* Make the http server listen on port 3000. */
http.listen(3000, function(){
console.log('listening on *:3000');
});

Related

Watching and running 2 scripts with Node, Concurrently and Typescript causes address in use

I'm trying to use concurrently with nodemon and tsc to have 2 TS scripts be watched, and thus my nodemon server updated when something changes, the index.ts is my router and the queue.ts is a worker. I'm using the following package.json
"dev": "concurrently -k yarn:watch yarn:serve:*",
"watch": "tsc -w",
"serve:index": "nodemon ./build/index.js",
"serve:queue": "nodemon ./build/queue.js"
When running yarn dev I get the following error
[serve:*queue] Error: listen EADDRINUSE: address already in use :::3000 even though the queue.js file only contains a console.log
In my index.ts file I'm using express as follows
import express from 'express'
const app = express()
app.use(express.json())
app.listen(3000, () => {
console.log('Running on 3000...');
console.log('For the UI, open http://localhost:3000/admin/queues');
});
This is my queue.ts file
console.log('testing');
When the script is not running the port is not in use, I check this by running lsof -i tcp:3000

Configure nodemon to refresh same tab

This could be really rookie. I want to know if there is a way that I can configure nodemon to refresh the same tab instead of opening a new tab each time I make a change in my js files.
nodemon is not able to do that. What you are looking for is something like browser-sync or LiveReload.js.
I use a package called reload. Assuming you are doing this on your FE and that you already installed express.js and nodemon
first install reload
npm install --save-dev reload
Than create a index.js file or server.js or whatever name your want
this is my index.js:
const express = require('express')
const http = require('http')
const reload = require('reload')
const opn = require('opn')
const app = express()
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'html')
app.set('src', './src')
app.use(express.static('src'))
app.get('/', (req, res) => res.render('index'))
const server = http.createServer(app)
server.listen(8080, function() {
console.log('Listening to port 8080...')
})
opn('http://localhost:8080')
reload(app)
the opn package can be intalled with
npm install opn
it will automatically open your localhost once you type npm start on your terminal
on HTML you have to insert something like this after the closing body tag:
</body>
<script src="/reload/reload.js"></script>
and on package.json the following:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js -e els,js,html,css,json"
},
The only problem with this is that it will reopen the browser every time you make a change to those files and well as reload the current opened one sure to the opn package. I am still going to create a function to prevent that, eventually. Hope that helps!
Its Simple.
To run your server use
npm start
instead of
nodemon server
Just run npm start nodemon server.
It works for me. Make sure you have latest nodemon version installed.

How to set up dev and API server from create-react-app?

I've started a new app with create-react-app, and ejected from that. I made a small express server as follows:
const express = require('express');
const app = express();
if(process.env.NODE_ENV === 'production') {
app.use(express.static('build'));
}
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server started at: http://localhost:${port}/`);
});
In package.json I've added a line, "proxy": http://localhost:3000", as well as switching the commands:
"scripts": {
"run": "npm-run-all -p watch-css start-js",
"start": "node server.js",
},
run used to be start.
However now of course when I run npm start and go to localhost:3000, I get Cannot GET /. I need this server to receive and return local API calls I'll be making from my app, but I also want it to run a hot-reloading dev server just like the old npm start (now npm run) command did. How do I do this?
Some time ago I made a fork of the create-react-app repository adding webpack watch option because of this same reason. It might help you.
Just to add more info, I really invested time looking on how to get webpackdevserver to build the "bundle.js", and found that it is not possible because it loads the bundle into memory but doesn't persist it, so the file is never created. The only way available is the webpack watch option but, I don't understand why the create-react-app team can't add it to the repo, it's a really requested feature, and there are more forks than mine that solves this issue. So, you have three options:
Use the proxy server in package.json (if it works)
Make your own fork and add the watch option, or use an existing one
Don't use create-react-app

How to make the webpack dev server run on port 80 and on 0.0.0.0 to make it publicly accessible?

I am new to the whole nodejs/reactjs world so apologies if my question sounds silly. So I am playing around with reactabular.js.
Whenever I do a npm start it always runs on localhost:8080.
How do I change it to run on 0.0.0.0:8080 to make it publicly accessible? I have been trying to read the source code in the above repo but failed to find the file which does this setting.
Also, to add to that - how do I make it run on port 80 if that is at all possible?
Something like this worked for me. I am guessing this should work for you.
Run webpack-dev using this
webpack-dev-server --host 0.0.0.0 --port 80
And set this in webpack.config.js
entry: [
'webpack-dev-server/client?http://0.0.0.0:80',
config.paths.demo
]
Note If you are using hot loading, you will have to do this.
Run webpack-dev using this
webpack-dev-server --host 0.0.0.0 --port 80
And set this in webpack.config.js
entry: [
'webpack-dev-server/client?http://0.0.0.0:80',
'webpack/hot/only-dev-server',
config.paths.demo
],
....
plugins:[new webpack.HotModuleReplacementPlugin()]
This is how I did it and it seems to work pretty well.
In you webpack.config.js file add the following:
devServer: {
inline:true,
port: 8008
},
Obviously you can use any port that is not conflicting with another. I mention the conflict issue only because I spent about 4 hrs. fighting an issue only to discover that my services were running on the same port.
Configure webpack (in webpack.config.js) with:
devServer: {
// ...
host: '0.0.0.0',
port: 80,
// ...
}
I am new to JavaScript development and ReactJS. I was unable to find an answer that works for me, until figuring it out by viewing the react-scripts code. Using ReactJS 15.4.1+ using react-scripts you can start with a custom host and/or port by using environment variables:
HOST='0.0.0.0' PORT=8080 npm start
Hopefully this helps newcomers like me.
Following worked for me -
1) In Package.json add this:
"scripts": {
"dev": "webpack-dev-server --progress --colors"
}
2) In webpack.config.js add this under config object that you export:
devServer: {
host: "GACDTL001SS369k", // Your Computer Name
port: 8080
}
3) Now on terminal type: npm run dev
4) After #3 compiles and ready just head over to your browser and key in address as http://GACDTL001SS369k:8080/
Your app should hopefully be working now with an external URL which others can access on the same network.
PS: GACDTL001SS369k was my Computer Name so do replace with whatever is yours on your machine.
I struggled with some of the other answers. (My setup is: I'm running npm run dev, with webpack 3.12.0, after creating my project using vue init webpack on an Ubuntu 18.04 virtualbox under Windows. I have vagrant configured to forward port 3000 to the host.)
Unfortunately putting npm run dev --host 0.0.0.0 --port 3000 didn't work---it still ran on localhost:8080.
Furthermore, the file webpack.config.js didn't exist and creating it didn't help either.
Then I found the configuration files are now located in build/webpack.dev.conf.js (and build/webpack.base.conf.js and build/webpack.prod.conf.js). However, it didn't look like a good idea to modify these files, because they actually read the HOST and PORT from process.env.
So I searched about how to set process.env variables and achieved success by running the command:
HOST=0.0.0.0 PORT=3000 npm run dev
After doing this, I finally get "Your application is running here: http://0.0.0.0:3000" and I'm finally able to see it by browsing to localhost:3000 from the host machine.
EDIT: Found another way to do it is by editing the dev host and port in config/index.js.
If you're in a React Application created with 'create-react-app' go to your package.json and change
"start": "react-scripts start",
to ... (unix)
"start": "PORT=80 react-scripts start",
or to ... (win)
"start": "set PORT=3005 && react-scripts start"
Following worked for me in JSON config file:
"scripts": {
"start": "webpack-dev-server --host 127.0.0.1 --port 80 ./js/index.js"
},
I feel dirty for telling you this b/c of the security implications of what you're trying to do, but here you go.
npm run dev -- -h 0.0.0.0 -p 80
For me: changing the listen host worked:
.listen(3000, 'localhost', function (err, result) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:3000');
});
was changed to :
.listen(3000, '0.0.0.0', function (err, result) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:3000');
});
and the server started listening on 0.0.0.0
I tried the solutions above, but had no luck. I noticed this line in my project's package.json:
"bin": {
"webpack-dev-server": "bin/webpack-dev-server.js"
},
I looked at bin/webpack-dev-server.js and found this line:
.describe("port", "The port").default("port", 8080)
I changed the port to 3000. A bit of a brute force approach, but it worked for me.
For me, this code worked. Just add it on your package.json file :
"scripts": {
"dev-server": "encore dev-server",
"dev": "webpack-dev-server --progress --colors",
"watch": "encore dev --watch",
"build": "encore production --progress"
},
And run the script "build" by running npm run build
For windows create file runMobile.bat
set PORT=8081
set HOST=192.168.3.20
npm run dev
I tried this to easily use another port:
PORT=80 npm run dev

How do I make Openshift to use Express 4, instead of its installed Express 3?

I developed my Nodejs Express app locally using Express 4 and it works as expected on my computer. I then git the whole app up to Openshift. When I try to run it Openshift returns"503 Service Unavailable". If I ssh into my base Node cartridge and do "express -V" it returns version 3.2.5. I get the same version 3.2.5 if I go into my app folder at app-root/repo and run "express -V".
So clearly my Express 4 which was included in the git upload in my app's node_modules is not being used. What is the solution to use Express 4 as required by my app?
Ideas are- remove Openshift's version of Express 3, force Openshift to use my Express 4 in my app area, upgrade Openshift's Express 3 to Express 4. I cannot figure out how to do any of those and I have researched this.
Here's how to troubleshoot:
ssh into your cartridge
cd into the app-root/repo directory
run grep version ./node_modules/express/package.json
you should see a version based on your package.json dependency
verify your package.json has a scripts section containing a start command that just runs your app with something like node ./server.js (server.js being whatever file you coded your main app start script in). You don't need the express command line program to launch an express server. It's for setting up new project boilerplate and other ancillary tasks.
To see the version of express running within your app, you can add this code to your server.js (or equivalent) file: console.log(require("express/package").version);
Look at this project to know how to integrate openshift with express4
Its a simple example .
https://github.com/master-atul/openshift-express4
try this
rhc ssh
cd app-root/repo
npm start
also edit the ./bin/www
var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || '8080');
var ip = process.env.OPENSHIFT_NODEJS_IP;
if (typeof ip === "undefined") {
// Log errors on OpenShift but continue w/ 127.0.0.1 - this
// allows us to run/test the app locally.
console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
ip = "127.0.0.1";
};
//app.set('ip', port);
app.set('port', port);
var server = http.createServer(app);
server.listen(port, ip);
server.on('error', onError);
server.on('listening', onListening);
you can follow step:
copy all content bin/www and replace all content in file server.js:
Change some content at server.js:
from
`var port = normalizePort(process.env.PORT || '3000');`
to
var port = normalizePort(process.env.OPENSHIFT_NODEJS_PORT || '3000');
Add line:
var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
from
server.listen(port);
to
server.listen(port, ip);
Add more to package.json
from
"scripts": {
"start": "node bin/www"
},
to
"scripts": {
"start": "node server.js"
},
Add line:
"main": "server.js",
Use npm install --save module-name for npm install
create file .gitignore with content:
node_modules
on local run node server.js to start server with address localhost:3000
upload to openshift:
git add .
git commit -m "First update new server version"
git push
Browser: domain-appname.rhcloud.com

Resources