Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http-server nodejs running but not working

I am trying to run http-server for nodejs.

After using npm start the server starts on the given port(8000) perfectly with a few ignorable errors. But when I try to run the application url, it says 'Webpage is not available'. When i tried to ping the IP it responds by sending packets perfectly.

When I run http-server command it shows that it is running on 127.0.0.1:8080 and 172.31.46.121:8080. I tried changing the localhost port to 8080,but with no difference in results.

I am using putty client to run linux on windows.

nodejs version-5.4.1

npm version-3.7.0

Please help..

There is one more thing..

I ran this server on putty at port 80. Then with the server still running i tried to run the same nodejs server using Bitnami client(i.e. using npm start) at the same port(80) and there was no conflict of ports. Even the app is running fine at that port when I run it through Bitnami client.

http-server file code-

` #!/usr/bin/env node

var colors = require('colors'),
httpServer = require('../lib/http-server'),
argv = require('optimist').argv,
portfinder = require('portfinder'),
opener = require('opener');

if (argv.h || argv.help) {
console.log([
"usage: http-server [path] [options]",
"",
"options:",
"  -p                 Port to use [8080]",
"  -a                 Address to use [0.0.0.0]",
"  -d                 Show directory listings [true]",
"  -i                 Display autoIndex [true]",
"  -e --ext           Default file extension if none supplied [none]",
"  -s --silent        Suppress log messages from output",
"  --cors             Enable CORS via the 'Access-Control-Allow-Origin'       header",
"  -o                 Open browser window after staring the server",
"  -c                 Set cache time (in seconds). e.g. -c10 for 10 seconds.",
"                     To disable caching, use -c-1.",
"  -h --help          Print this list and exit."
 ].join('\n'));
process.exit();
}

var port = argv.p || parseInt(process.env.PORT, 10),
host = argv.a || '0.0.0.0',
log = (argv.s || argv.silent) ? (function () {}) : console.log,
requestLogger;

if (!argv.s && !argv.silent) {
requestLogger = function(req) {
log('[%s] "%s %s" "%s"', (new Date).toUTCString(), req.method.cyan,     req.url.cyan, req.headers['user-agent']);
}
}
if (!port) {
portfinder.basePort = 8080;
portfinder.getPort(function (err, port) {
if (err) throw err;
listen(port);
});
} else {
listen(port);
}

function listen(port) {
var options = {
root: argv._[0],
cache: argv.c,
showDir: argv.d,
autoIndex: argv.i,
ext: argv.e || argv.ext,
logFn: requestLogger
};

if (argv.cors) {
options.headers = { 'Access-Control-Allow-Origin': '*' };
}

var server = httpServer.createServer(options);
server.listen(port, host, function() {
log('Starting up http-server, serving '.yellow
  + server.root.cyan
  + ' on port: '.yellow
  + port.toString().cyan);
 log('Hit CTRL-C to stop the server');

if (argv.o) {
  opener('http://127.0.0.1:' + port.toString());
}
});
}

if (process.platform !== 'win32') {
//
// Signal handlers don't work on Windows.
//
process.on('SIGINT', function () {
log('http-server stopped.'.red);
process.exit();
});
}

`

like image 871
Naman Gupta Avatar asked Oct 18 '22 16:10

Naman Gupta


1 Answers

Please provide some code, out of pure speculation there may be an issue with bind IP ie. you may have your IP address of server bind to 127.0.0.1 which can only be accessed from locahost, change it to 0.0.0.0 to allow access from outside.

var http = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, {
          'content-type': 'text/plain'
});
res.end('It works');
}).listen(3000, '0.0.0.0');
like image 168
AJS Avatar answered Nov 01 '22 17:11

AJS