user2470478
user2470478

Reputation:

how to create a Web-Service in Node-JS

HI friends ... this might be a silly question but its hard for a beginner like me . I am trying to lean about webservices I have created a sample program in express that outputs a JSON

var http=require('http');
var express=require('express');


var app=express();


app.configure(function(){
    app.set('port',process.env.PORT || 5000);
    app.use(express.bodyParser());
});


app.get("/",function(req,res){

    res.json({Message:"HELLO_WORLD"});
});



http.createServer(app).listen(app.get('port'),function(){
    console.log("Server is running at port:" + app.get('port'));
});

when i give localhost:5000 it dosen't work. According to my knowledge the solution is WEBSERVICES .


how to create it .... what additional lines of code i need to add for this existing code. or if i need to create any other configuration file .... how to do it? . Thanks in advance

Upvotes: 1

Views: 289

Answers (3)

Rikky
Rikky

Reputation: 515

What is your error message, is it EADDINUSE? it's mean another version of your code still running and your port now in use, or some other application is using the port 5000. You should turn off your code or change the port

Upvotes: 1

Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16030

Your code works just fine. Make sure you have express installed in the node_modules directory on the pc from which you are trying to execute the code.

Upvotes: 0

bodokaiser
bodokaiser

Reputation: 15752

Just use app.listen not http.createServer

var app = express();

app.get('/', function(req, res) {
  res.send(200);
});

app.listen(3000);

Upvotes: 0

Related Questions