在没有jsonfile模块的情况下使用ExpressJS在本地创建JSON文件

时间:2017-06-08 12:02:00

标签: javascript json node.js express

我想通过ExpressJS在本地存储的JSON文件中创建对象的条目,而不使用jsonfile npm模块。这里需要一些帮助。该功能将写在注释区域中。

var express=require('express');
var app=express();
var cookieParser=require('cookie-parser');
var bodyParser=require('body-parser');

app.use(bodyParser.json({limit:'10mb',extended:true}));
app.use(bodyParser.urlencoded({limit:'10mb',extended:true}));
var users=require('./users.json');

var myArray=[];

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

app.post('/mydata/create',function(req,res){
	console.log(req.body);
	myArray.push(req.body.value);
	res.send(myArray);
});

app.put('/mydata/createjson',function(req,res,val){
	//function to be written here
})

app.put('/mydata/:value/edit',function(req,res){

});

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

app.listen(4000,function(){
	console.log('listening on 4000');

});

这是users.json

[{
    "mobileNumber": 1234567890,
    "email": "newmail@gmail.com",
    "lastName": "Watson",
    "firstName": "George",
    "userName": "GeorgeWatson1"
},
{
    "mobileNumber": 0987654321,
    "email": "abc@xyz.com",
    "lastName": "Banks",
    "firstName": "John",
    "userName": "JohnBanks1"
}
]

假设要输入

[
{
    "mobileNumber": 0987654321,
    "email": "jhg@xyz.com",
    "lastName": "abc",
    "firstName": "xyz",
    "userName": "xyzabc1"
}
]

0 个答案:

没有答案