0

Node js server has been started but not responding when I hit it from web browser

Navit
  • 1

2 Answers2

0

Why would you use apache web server and node.js?

You can create a very simple web server using node.js

const http = require('http');

http.createServer((request, response) => {
  const { headers, method, url } = request;
  let body = [];
  request.on('error', (err) => {
    console.error(err);
  }).on('data', (chunk) => {
    body.push(chunk);
  }).on('end', () => {
    body = Buffer.concat(body).toString();
    // At this point, we have the headers, method, url and body, and can now
    // do whatever we need to in order to respond to this request.
  });
}).listen(8080); // Activates this server, listening on port 8080.

If you're thinking on using Node.js there are plenty of framework to use, such as Express, Hapi, Resitfy, Fastify etc...

hope that's help.

Doron Segal
  • 2,242
  • 23
  • 22
  • var https = require('http'); var fs = require('fs'); console.log('test data'); https.createServer( function (req, res) { console.log('In the main page'); res.writeHead(200); res.end("hello world\n"); }).listen(8000); this is my code but not showing 'In the main page' in console also 'hello world' on the web browser – Navit Jan 09 '19 at 06:03
  • I would suggest you disable apache and just use plain node.js but if you really need to use apache server for proxying your node.js app this might be useful: https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html as @yudu suggested. – Doron Segal Jan 09 '19 at 06:54
0

You need "reverse proxy" to achieve this.

With config like:

ProxyPass "/"  "http://localhost:8888"

Replace 8888 with the real port your nodejs app listening.

Check apache's guide here: https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html.

Also, check another related question: How to run nodejs application in apache server

banyudu
  • 1,046
  • 9
  • 19