Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs http://localhost:8080 not working

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.

like image 554
Muhammed Raheez PC Avatar asked Apr 21 '26 02:04

Muhammed Raheez PC


1 Answers

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)
like image 196
Arzoo Avatar answered Apr 22 '26 16:04

Arzoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!