码迷,mamicode.com
首页 > 其他好文 > 详细

node开发

时间:2016-02-26 06:54:37      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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);

node开发

标签:

原文地址:http://www.cnblogs.com/jayruan/p/5218911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!