简体   繁体   English

带有http的json的Node.js服务器

[英]Node.js server with http get json

I want to create a simple Node.js server to do the following : 我想创建一个简单的Node.js服务器来执行以下操作:

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. 使用我的应用程序,我只需执行命令http.get(Node.Js_Server_address/json)即可获取存储在服务器上的json文件数据。

Could please help me with a tutorial? 能帮我个教程吗? Any help would be appreciated! 任何帮助,将不胜感激!

This is very simple example of node.js server: 这是一个非常简单的node.js服务器示例:

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. 这里这里都有一个不错的教程...您可以使用npm安装node.js及其所需的所有软件包。 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. 您可以通过nodeJs的express模块​​创建REST服务器。 In your server folder use npm install express to download express module. 在服务器文件夹中,使用npm install express下载Express模块​​。 You can get more information about express from here . 您可以从此处获取有关express的更多信息。 After that create a server.js file in your server folder.In server.js 之后,创建一个server.js在服务器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. 让我知道这是否对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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