简体   繁体   中英

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM