如何在 Visual Studio 2015 年编写 API 方法 Nodejs Express 3 应用程序
How To Write API Methods Nodejs Express 3 Application In Visual Studio 2015
如何在 node.js express 3 应用程序中编写 API 方法。
我的 app.js 看起来像:
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var cons = require('consolidate');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.engine('html', cons.swig)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
我正在使用html视图引擎,请帮助我,因为我是这里的新手。
仅供参考,请参阅以下方式:
routes.js |保持单一 route.js 或根据功能为不同的路线创建多个文件
var api = {}; // so that if apis grow just add like, api.inbox, api.share ..
api.comment = require('./api/comment');
exports.likeComment = function (req, res) {
api.comment.likeComment(req, res);
}
exports.unlikeComment = function (req, res) {
api.comment.unlikeComment(req, res);
}
//api object itself can be exported, its upto you what to choose
api/comment.js | api文件夹将包含comment.js、inbox.js等文件..这些文件包含api方法,在此处编写逻辑
//method to be called when comment is liked
exports.likeComment = function (req, res) {
//code here
}
//method to be called when commend is unliked
exports.unlikeComment = function (req, res) {
//code here
}
app.js |它可以在 app.js 中,也可以在其他一些路由配置文件中,并且在 app.js 中可能需要该文件,这又取决于您选择什么
var routes = require('./routes');
//like comment api
app.post('/comment/like', function(req, res, next) {
routes.likeComment(req, res);
});
//unlike comment api
app.post('/comment/unlike', function(req, res, next) {
routes.unlikeComment(req, res);
});
编辑初学者起床运行宁Github Repo | Basic api methods using express.js下载运行node app.js
快乐帮助!
如何在 node.js express 3 应用程序中编写 API 方法。 我的 app.js 看起来像:
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var cons = require('consolidate');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.engine('html', cons.swig)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
我正在使用html视图引擎,请帮助我,因为我是这里的新手。
仅供参考,请参阅以下方式:
routes.js |保持单一 route.js 或根据功能为不同的路线创建多个文件
var api = {}; // so that if apis grow just add like, api.inbox, api.share ..
api.comment = require('./api/comment');
exports.likeComment = function (req, res) {
api.comment.likeComment(req, res);
}
exports.unlikeComment = function (req, res) {
api.comment.unlikeComment(req, res);
}
//api object itself can be exported, its upto you what to choose
api/comment.js | api文件夹将包含comment.js、inbox.js等文件..这些文件包含api方法,在此处编写逻辑
//method to be called when comment is liked
exports.likeComment = function (req, res) {
//code here
}
//method to be called when commend is unliked
exports.unlikeComment = function (req, res) {
//code here
}
app.js |它可以在 app.js 中,也可以在其他一些路由配置文件中,并且在 app.js 中可能需要该文件,这又取决于您选择什么
var routes = require('./routes');
//like comment api
app.post('/comment/like', function(req, res, next) {
routes.likeComment(req, res);
});
//unlike comment api
app.post('/comment/unlike', function(req, res, next) {
routes.unlikeComment(req, res);
});
编辑初学者起床运行宁Github Repo | Basic api methods using express.js下载运行node app.js
快乐帮助!