标签:
node 开发流程
1,express nodeName ; npm install;
2,加入Database;var mongoose = require(‘mongoose‘); npm install mongoose --save
var dbIpAddress = process.env.MONGO_PORT_27017_TCP_ADDR || ‘localhost‘;
var dbPort = process.env.MONGO_PORT_27017_TCP_PORT || ‘27017‘;
mongoose.connect(‘mongodb://‘ + dbIpAddress + ‘:‘ + dbPort + ‘/menusifu‘, function(err) {
if(err) {
console.log(‘connection error‘, err);
} else {
console.log(‘DB connection successful‘);
}
});
3,加入Log
var logDirectory = __dirname + ‘/log‘;
var phonePath=path.join(__dirname, ‘../public/‘+d.merchantId+"/txt");
mkdirp(phonePath, function (err) {
if (err) console.log(err)
else console.log(‘success‘);
});
4,app.use前加入过滤
app.use(function (req, res, next) {
res.header(‘Access-Control-Allow-Origin‘, ‘*‘);
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header(‘Access-Control-Allow-Methods‘, ‘GET, POST, OPTIONS, PUT, PATCH, DELETE‘);
next();
});
app.use(‘/‘, routes);
5,把ERROR改成
var errorJson={
"11000":"duplicate key error"
}
if (app.get(‘env‘) === ‘development‘) {
app.use(function(err, req, res, next) {
if(!!err.code){err.message=!!errorJson[err.code]?errorJson[err.code];}
res.json({"ok":0,"message":err.message});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
if(!!err.code){err.message=!!errorJson[err.code]?errorJson[err.code];}
res.json({"ok":0,"message":err.message});
});
=======================================
if (!!err) return next(err);
标签:
原文地址:http://www.cnblogs.com/jayruan/p/5218911.html