我想從服務器獲取以.json文件.js文件在客戶方JSON響應
var fs = require('fs');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({extended: false, limit: '50mb'});
module.exports = function(app){
app.get('/tables_particular/', function(req, res){
first = JSON.parse(fs.readFileSync('./database/table.json'))
res.render('tables_particular', {todos: data.member,
tbledit: first,
});
});
app.post('/tables_particular/', urlencodedParser, function(req, res){
var exportlist = req.body.detail
fs.writeFileSync('./database/table.json', exportlist);
res.json(exportlist);
});
這裏是app.js:(服務器代碼)
var express = require('express');
var mShController = require('./controllers/mShController');
var app = express();
//
app.set('view engine', 'ejs');
//static files
app.use(express.static('./public'));
mShController(app);
//listen to port
app.listen(3000);
console.log('You are listening to port 3000');
我需要在JavaScript文件中使用哪些代碼來獲取json文件。
我需要在js文件:
var data = response.tbledit
如果'myfile.json'是靜態的,你可以'常量JSON =要求( 'myfile.json')' –