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.

2

1 Answer 1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.