3

I am beginner in nodejs, learning from w3school. So previously I worked in PHP. and the thing is that when I run a simple nodejs program nothing happening in command line and web browser

var http = require('http');

http.createServer(function(request, response) {

    request.on('readable', function() {
        request.read(); // throw away the data
    });

    request.on('end', function() {

        response.writeHead(200, {
            'Content-Type': 'text/plain'
        });

        response.end('Hello HTTP!');
    });

}).listen(8080);  

I already use xampp server for apache.

how will run both in system.

Muhammed Raheez PC
  • 393
  • 2
  • 4
  • 19
  • what is command you tried to execute? For changing the Xampp server port https://stackoverflow.com/questions/11294812/how-to-change-xampp-apache-server-port – Sathishkumar Rakkiyasamy Feb 14 '18 at 04:32
  • if port(8080) is already assigned then you can not user the same port try to change your port and check. – Priyank Feb 14 '18 at 04:35

1 Answers1

2

If You are using express framework of Node js then try with following code to run NodeJs Program in app.js (main js file of ur NodeJs Application)

const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(8080, function () {
  console.log('Example app listening on port 8080!')
})

then from command prompt run this command

-> node (path to app.js)
Arzoo
  • 106
  • 5