Doapper
Doapper

Reputation: 109

Node.js server with http get json

I want to create a simple Node.js server to do the following :

With my application I just do the command http.get(Node.Js_Server_address/json) to get the json file data stored on my server.

Could please help me with a tutorial? Any help would be appreciated!

Upvotes: 0

Views: 1782

Answers (2)

ozata
ozata

Reputation: 562

There are lots of examples on this topic, i think you should make some googling before next time. You can create a REST server via express module of nodeJs. In your server folder use npm install express to download express module. You can get more information about express from here. After that create a server.js file in your server folder.In server.js

var express = require('express');
var app = express();
var PORT = 8080;
/* req stands for request, res stands for response */
app.get('/json',function(req,res){
    res.json(yourData);
})

app.listen(PORT,function(){
    console.log('Express is listening port:' + PORT + '!');
})

So this should do the work. Let me know if this helps you.

Upvotes: 1

Adi Azarya
Adi Azarya

Reputation: 4493

This is very simple example of node.js server:

var app = require('./app');
var http = require('http');
var server = http.createServer(app);

server.listen(8080, function() {
    console.log("listening to: http://127.0.0.1:8080");
});

// routing
app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
});

there is a nice tutorial here and here ... you can use npm to install node.js and all the packages that you need for it. hope it helps.

Upvotes: 1

Related Questions