Usually develop front-end application, if no existing back-end interface to debugging, but also to ensure front-end schedule, how to do it, of course, still a lot of ways, many large cattle have shared a lot of experience, I may as well say that I am commonly used method.
Request local data files
The local data in a program specified directory, launch http request when the get request to the specified file directory
Jquery ajax to the example:
postRequest: function(model, async) {
if(!model) {
return null;
}
if(Common.urlParams.d) {
url = "../data/" + model.method + ".json";
model.type = "GET";
} else {
url = service-api-path + model.method; //service-api-path是后端接口公共地址
}
return $.ajax({
async: (async == undefined || async) ? true : false,
url: url,
type: model.type,
dataType: "json",
timeout: 30000,
data: model.params,
beforeSend: function(x, settings) {
//todo..
},
complete: function(x, status) {
//todo..
},
error: function(x, h, r) {
//todo
}
});
}
This example is added when accessing a page in the link d parameter, the parameter values any, you can call the local data, this method can only read the file, the content is relatively rigid, Here to talk about another way
With nodejs + express
With nodejs get hold of a web server is often gray simple, this is a lot of developers will be using nodejs transit services, how to build testing services under the following description
app.js
var path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
autoRoutes = require('express-auto-routes'),
server = require('./server');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
var routes = autoRoutes(app);
app.use(server);
app.use(function(req, res, next) {
res.status(404);
next({ _code: 404, _msg: 'Page not found' });
});
app.use(function(err, req, res, next) {
console.error(err);
if (err._status) res.status(err._status);
res.json({
_code: err._code || 1,
_msg: err._msg || err
});
});
var server;
if (!module.parent) {
var PORT = 8989;
console.log('[INFO] Msg board RESTful API listening at localhost:%s', PORT);
server = app.listen(PORT);
} else {
module.exports = app;
}
server.js
var dbm = require('./dbm'),
_ = require("lodash");
module.exports = function(req, res, next) {
let data = [],
params = {},
url = req.originalUrl,
db = new dbm(`./db/${url.slice(url.lastIndexOf('/') + 1, url.length)}.json`).read()
data = _filter(db, req.body)
res.json({ timestamp: new Date().getTime(), msg: "查询成功", isSuccess: 0, data: data })
}
const _filter = (db, obj) => {
if (!obj) return
return _.filter(db, o => { return obj.dateTime == o.dateTime })
}
dbm.js
var fs = require('fs'),
path = require('path');
function dbm(relativePath) {
this.db = path.resolve(__dirname, relativePath);
}
dbm.prototype._isAvailable = function() {
return fs.existsSync(this.db);
};
dbm.prototype.read = function() {
if (!this._isAvailable()) return null;
var contentInStr = fs.readFileSync(this.db, 'utf-8'),
content;
try {
content = JSON.parse(contentInStr);
} catch (e) {
//this.delDb();
console.error('[ERR] JSON.parse failed, deleted ' + this.db);
}
return content || null;
};
dbm.prototype.save = function(data) {
var stringToSave = JSON.stringify(data);
if (!stringToSave) return;
fs.writeFileSync(this.db, stringToSave, 'utf-8');
};
dbm.prototype.delDb = function() {
try {
fs.unlinkSync(this.db);
} catch (e) {
console.error('DB file does not exist');
}
};
module.exports = dbm;
demo.json
[{
"name": "jack",
"age": 18,
"id": "124443",
"dateTime": "20170101"
},
{
"name": "tom",
"age": 21,
"id": "1232323",
"dateTime": "20170103"
},
{
"name": "alix",
"age": 22,
"id": "123232323",
"dateTime": "20170102"
}
]
Of this, the server basically get away, json file storing content in accordance with mongodb or in accordance with mysql data records on the line, the introduction of an array of store-operated lodash complete CRUD completely goes without saying, of course, can be extended to become a real server Well, after all, a lot of projects direct the development of the server is nodejs
Complete example: Primary:https://github.com/dawnyu/node-simple-server.git
http://dawns.me/2017/05/05/%E7%94%A8nodejs+express%E6%90%AD%E5%BB%BA%E5%89%8D%E7%AB%AF%E6%B5%8B%E8%AF%95%E6%9C%8D%E5%8A%A1%E7%AB%AF/