I'm trying to build a Node.JS server to listen on port 3800 of my CentOS server this way:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.send('<h1>Hello World!</h1>');
});
http.listen(3800, function(){
console.log('Listening on port: 3800');
});
I have a domain and I configured this domain on my apache server with virtual host to listen on port 80. But when I try to access
http://example.com:3800 or http://server_ip:3800
it's not working. The browsers keeps trying to connect and then I got the error.
I don't know what I did wrong, since I followed the tutorial. I searched other questions here, I tried to copy the code into my index.js and nothing. This simple "Hello World" is not showing and I can't access the server.
I did the "node index.js" and on my server is showing "Listening on port: 3800" perfectly, I have root access and I did everything with the root user. I did the "npm install express" and "npm install socket.io" commands too, and I tried to make the package.json file and then "npm install". I searched another website and I tried their instructions with "npm install --save: express", won't work too.
I think it's a problem with my Linux configuration.
My question is: how I can make this simple script work when I access http://example.com:3800?
The correct answer to my question is to open the port at the iptables using this command:
iptables -I INPUT -p tcp --dport 3800 -j ACCEPT
And then saving it with the command:
/sbin/service iptables save
CentOS6 based system.
Your actual example will work going to localhost:3800
Im not sure how is that of running nodejs in an apache server :/
if it helps, I normally do it slightly different than your code:
var app = require('express');
var http = require('http');
app.get('/', function(req, res){
res.send('<h1>Hello World!</h1>');
});
app.listen(3800, function(){
console.log('Listening on port: 3800');
});
EDIT-------------------------------
Your main issue here is that you are trying to use nodejs in Apache as I mention above, is not impossible but im sure is a pain to configure your server, and will not be the normal LAMP stack configuration, and you will probably run in some kind of bugs or cutted functionalitys, try to host your app in a node backend, something like openshift or heroku
Related
I have a debian server running on Google Compute Engine with a host like example.com and I'm trying to run a node.js app on a directory like example.com/mynodeapp.
Node.js, NPM are installed properly on the debian server.
I'm running pm2 start main.js from the root of example.com/mynodeapp and everything running but when I go to example.com/mynodeapp, I have nothing, just the indexing of the files.
Express.js configure
main.js (entry)
var express = require('express')
var vhost = require('vhost')
express()
.use(vhost('example.com/mynodeapp', require('./dist/index').app))
.listen(8080)
dist/index.js
var express = require('express')
var app = express()
app.get('/', function(req, res) {
res.send('Hello World!');
})
exports.app = app
With .listen(8080) the port is set to 8080, so you'll have to change that or try example.com:8080.
Note that you will run into one of two problems, depending on your choice: Port 8080 is probably not open – you'd have to allow it in the firewall.
If you're currently getting a file listing on port 80, there's some other server running (possibly apache or nginx from the standard debian install). You will have to stop that server to free up the port.
I've been experimenting with Node.js on my windows machine and also my linux machine to create a web server using node.
If I try my code on windows and activate my server with "node server.js" and navigate to localhost:8080 I am met with "Hello World!" as intended.
But if I try on my linux box with the same code it shows me the directory instead of serving the page. Not sure what's going on here!
Here's the code I've been using
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
I've tried using my local ip to the machine and also using the domain followed by ":8080", both give me the same results.
I cant find anybody else with the same problem and I've been looking for hours.
I got the same issue while manually starting server from windows machine.
using command http-server -p 4200
Say, I was trying to start the server from D:/ProjectName. When I changed that and started from D:/ProjectName/build, it was working fine. Build folder has the index file and all other files.
I am new to expressjs and am trying to get my express application (done with the express generator) working on my website, I currently uploaded the directory which is is contained in like so..
http://www.example.com/express-app-here
so I could see it working online. However, when I navigate to where the App is, I seem to only get the directory structure, and express isn't routing me to the appropriate place like it is when I go to localhost:3000.
I take it this has something to do with the fact that express isn't executing my application? Locally,
npm start
needs to be run on the console in order to get it to run, is there some kind of log I need to execute this command in? Or something I need to change in the app.js or /bin directory?
As it was said in the comments, you need to have nodejs installed on your server. It's not as simple as just copying the node app directory over to the server.
You will have to install node and npm on the server, and then run your app from the server, probably using npm start like you were doing on your local machine.
From there, you will want to go into your app code and make sure a route exists for /express-app-here unless you want www.example.com:3000 to take you directly to the express app.
Basically do it like this:`
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var port = process.env.PORT || config.webServer.port || 3000;
server.listen(port, function () {
console.log('server running');
console.log(port);
console.log(server);
});
exports.module = exports = app;
save it app.js
Go to path via cmd. Now run:-
1)npm install express
2)npm install http
3)node app.js
Will be enough to run express server
I locally wrote a nodeJS app using socket.io and express modules.
I wanted to use openshift for hosting.
So I changed the main .js to server.js which seems to be the index equivalent of the openshift file and changed the server port setting to:
var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
as indicated in some posts.
However after git commit, I am still getting:
remote: info: socket.io started
remote: warn: error raised: Error: listen EACCES
remote: DEBUG: Program node server.js exited with code 0
remote:
remote: DEBUG: Starting child process with 'node server.js'
and the website doesn't work.
As the app is serving a html file, there are two more places, where the port is mentioned, which sit in the index.html that is served:
header:
<script src='//localhost:3000/socket.io/socket.io.js'></script>
and within javascript for the html file:
var socket = io.connect('//localhost:'+process.env.OPENSHIFT_NODEJS_PORT || 3000);
// intial vars and multi list from server
socket.on('clientConfig', onClientConfig);
All files and modules are seemingly uploaded, but the EACCES error still prevails.
I get the feeling that maybe the header link to localhost:3000 might be the skipping point, but I am not sure. Anyone have any idea, what the problem is?
Also, there is no : socket.io/socket.io.js file in the socket.io modules folder, which I find confusing.
I had recently developed a chat client application using socket.io and also had webrtc in it. I was able to deploy the app on openshift by making the following changes into code.
Client Side
Keep the include script tag in a relative manner like so
<script src="/socket.io/socket.io.js"></script>
While declaring io.connection, change the ip part to point the application to server in this format.
var socket = io.connect('http://yourapp-domain.rhcloud.com:8000/', {'forceNew':true });
8000 is for http and 8443 is for https
Server Side
The io and the server should both be listening on the same port and the order in which the statements are run should also be given attention.
Step 1: Declare the http server using app.
( app is obtained from express)
var express = require('express');var app = express();)
var server = require('http').Server(app);
Step 2:
Declare io from socket.io and combine it with the server object.
var io = require('socket.io').listen(server);
Step 3:
Now, allow the server to listen to openshift port and ip.
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
Please pay special attention to the order of the statements you write, it is the order which causes issues.
The server side of your websocket needs to listen on port 8080 on your openshift ip address, the CLIENT side needs to connect to your ws://app-domain.rhcloud.com:8000
I have a few notes on how to use WebSockets here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#websockets
You don't need any additional server-side changes after adapting your code to take advantage of environment variables (when available)
OpenShift's routing layer exposes your application on several externally-accessible ports: 80, 443, 8000, 8443.
Ports 8000 and 8443 are both capable of handling websocket connection upgrades. We're hoping to add support for WebSocket connections over ports 80 and 443 soon.
This is driving me crazy... while I have a working version of Express + Socket.io, I can't seem to reproduce it with out-of-the-box NPM installs in a new project folder. Can anyone point out what I'm missing...? Here's my process:
I create a node_modules folder in my project directory (pwd), then do:
npm install express
npm install socket.io
Running those two commands puts the packages in my project's node_modules folder as expected. Now I set up my server with the following:
var express = require('express'),
server = express.createServer().use( express.static(__dirname+'./public') ).listen( 8080 ),
io = require('socket.io').listen(server);
My public folder contains static assets for my application. My public index HTML page includes a script tag for:
<script src="/socket.io/socket.io.js"></script>
Finally, I run my server script and go to the application in a web browser. My static public files are all served properly, however I get a 404 for /socket.io/socket.io.js. Now, I can swap in an express package from another old project and have this whole system work. Somehow that package instance is configured differently, but I can't figure out how to reproduce that. The Express website mentions something about installing dependencies, although running npm install -d doesn't seem to help (is there a specific pwd that I need to be in while running npm install -d?). I figure I must be missing something important about configuring a new Express instance after installing it with NPM.
Thanks for any and all insight!
Okay, so my example was actually an abbreviation of my code, and that example code does actually work. My real code with problems was a bit more cluttered, like so:
var server = express.createServer();
server
.use( server.router )
.use( express.static(__dirname+'/../public') )
.get('/api', function(req, res) {
res.write('API');
});
server.listen(8080);
var io = require('socket.io').listen(server);
I fixed the above code by doing the following:
server = server.listen(8080);
Apparently the listen command wraps the server object with some additional functionality. My originally posted shorthand example actually does work because listen is chained onto the final return into the server variable. Interesting little nuance.