简体   繁体   English

在NodeJS中使用JSON中的数据

[英]using data from JSON in NodeJS

Hello i just started learning Nodejs and made a local server as a start then i saw that most nodejs apps have config and package files i couldnt find any info on how to do a simple one or use JSON files so i tried myself this is what i got so far 您好,我刚刚开始学习Nodejs并以本地服务器作为开始,然后我看到大多数nodejs应用程序都具有配置和打包文件,我找不到有关如何做一个简单的或使用JSON文件的任何信息,所以我尝试了一下,这就是我到目前为止

this is the server file 这是服务器文件

var http = require('http');
var json = require('./package');
var fs = require('fs');

var server = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(addr.port);

console.log('server listening at', addr.address + ':' + addr.port);

and this is the json file 这是json文件

{
    "addr": {
        "address":"http://127.0.0.1",
        "port":"8081"
    }
}

i know that it will work with json.address and json.port but when i added "addr" i thought it would simplify things with addr.port 我知道它将与json.address和json.port一起使用,但是当我添加“ addr”时,我认为它将使用addr.port简化操作

so in short an explanation would be generously accepted on why it wont/shouldnt work or what im doing wrong 简而言之,对于它为什么不/不应该工作或我做错了什么,我们将慷慨地接受一个解释。

First of you should have a look at some tutorials or introduction sites like: 首先,您应该看一下一些教程或介绍网站,例如:

https://www.w3schools.com/nodejs/default.asp https://www.w3schools.com/nodejs/default.asp

Second: The package.json file is the main configuration file of your nodeJS application. 第二: package.json文件是您的nodeJS应用程序的主要配置文件。 Thats the config file that defines your start point of your application as well as all included modules. 多数民众赞成在配置文件,定义您的应用程序的起点以及所有包含的模块。 simply use npm init to create a default package.json file with basic information. 只需使用npm init创建具有基本信息的默认package.json文件。

Third: If you require a json into your application as you did in your example the JSON is included hierarchically. 第三:如果您像示例中那样在应用程序中需要JSON,则JSON会分层包含。 Wich means The object you required has an attribute addr which itself is a new object with an attribute address . Wich表示您需要的对象具有属性addr ,它本身就是带有属性address的新对象。

So the correct way to access your information is json.addr.address based on your object description 因此,根据您的对象描述,访问信息的正确方法是json.addr.address

you could also do something like this: 您还可以执行以下操作:

 var network = require('./settings').addr; console.log("ip => " + network.address); console.log("port => " + network.port); 

You need to list the parent object. 您需要列出父对象。 You have put addr.address and addr.port, this means you are directly trying to access the addr object, but the this object doesn't exist. 您已经放置了addr.address和addr.port,这意味着您直接尝试访问addr对象,但是this对象不存在。 Try doing json.addr.address and json.addr.port and it should work. 尝试做json.addr.address和json.addr.port,它应该可以工作。

var http = require('http');
var json = require('./package');
var fs = require('fs');

var server = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(json.addr.port);

console.log('server listening at', json.addr.address + ':' + json.addr.port);

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

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