- 浏览: 2560113 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
NodeJS RESTful Service with Gulp and Express
How to make ExpressJS and Services Layer working together?
Here is my app.js
/*jslint node: true */
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var router = require('./route');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', router);
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'));
console.log('Running server on port ' + app.get('port') + ' .');
Here is the route.js which is my router, but also I construct the services layer here as well.
/*jslint node: true */
'use strict';
var express = require('express');
var config = require('./lib/config');
var log4js = require('log4js');
var HashMap = require('hashmap').HashMap;
var emailscan = require('./lib/emailservice');
var account_controller = require('./controller/account.controller.js');
var job_controller = require('./controller/job.controller.js');
var attachment_controller = require('./controller/attachment.controller.js');
//hold all the services
var services = new HashMap();
//set up config
config.load(__dirname + '/config', 'test-conf');
services.set('config', config);
//set up logger
log4js.configure(__dirname + '/config/log/' + config.data.logConfigFileName, config.data.log4js);
var logger = log4js.getLogger('root');
services.set('logger', logger);
//set up emailscan
emailservice.services = services;
emailservice.init(services);
services.set('emailscan', emailscan);
//set up controllers
account_controller.init(services);
job_controller.init(services);
attachment_controller.init(services);
//routing configuration
var router = express.Router();
router.get('/accounts/:email', account_controller.getAccountByEmail);
router.post('/jobs/scan', job_controller.scanJobs);
router.get('/jobs/:accountId/:messageId', job_controller.getJob);
module.exports = router;
Here is one example of the controller, which is account.controller.js
/*jslint node: true */
'use strict';
var HashMap = require('hashmap').HashMap;
var services = null;
module.exports = {
getAccountByEmail : function(req, res) {
//services
var emailscan = services.get('emailservice');
var logger = services.get('logger');
//params
var email = req.params.email;
emailscan.fetchAccountInfoByEmail(email, function(err, account){
logger.debug("account info = " + account.id);
res.json(account);
});
},
init : function(myservices){
services = myservices;
}
};
Warning Info:
body-parser deprecated bodyParser: use individual json/urlencoded middlewares app.js:8:9
body-parser deprecated undefined extended: provide extended option node_modules/body-parser/index.js:105:29
Solution:
Change the body parser part as follow:
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
References:
should.js provide the test assertion
http://unitjs.com/guide/should-js.html
underscore enhance the JS operation
http://underscorejs.org/#filter
online JSON editor to validate the response
http://www.jsoneditoronline.org/
older blog about RESTful
http://sillycat.iteye.com/blog/2067546
http://sillycat.iteye.com/blog/2155801
http://sillycat.iteye.com/blog/2072384
https://github.com/luohuazju/easy/tree/master/easynodejs/buglist
How to make ExpressJS and Services Layer working together?
Here is my app.js
/*jslint node: true */
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var router = require('./route');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', router);
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'));
console.log('Running server on port ' + app.get('port') + ' .');
Here is the route.js which is my router, but also I construct the services layer here as well.
/*jslint node: true */
'use strict';
var express = require('express');
var config = require('./lib/config');
var log4js = require('log4js');
var HashMap = require('hashmap').HashMap;
var emailscan = require('./lib/emailservice');
var account_controller = require('./controller/account.controller.js');
var job_controller = require('./controller/job.controller.js');
var attachment_controller = require('./controller/attachment.controller.js');
//hold all the services
var services = new HashMap();
//set up config
config.load(__dirname + '/config', 'test-conf');
services.set('config', config);
//set up logger
log4js.configure(__dirname + '/config/log/' + config.data.logConfigFileName, config.data.log4js);
var logger = log4js.getLogger('root');
services.set('logger', logger);
//set up emailscan
emailservice.services = services;
emailservice.init(services);
services.set('emailscan', emailscan);
//set up controllers
account_controller.init(services);
job_controller.init(services);
attachment_controller.init(services);
//routing configuration
var router = express.Router();
router.get('/accounts/:email', account_controller.getAccountByEmail);
router.post('/jobs/scan', job_controller.scanJobs);
router.get('/jobs/:accountId/:messageId', job_controller.getJob);
module.exports = router;
Here is one example of the controller, which is account.controller.js
/*jslint node: true */
'use strict';
var HashMap = require('hashmap').HashMap;
var services = null;
module.exports = {
getAccountByEmail : function(req, res) {
//services
var emailscan = services.get('emailservice');
var logger = services.get('logger');
//params
var email = req.params.email;
emailscan.fetchAccountInfoByEmail(email, function(err, account){
logger.debug("account info = " + account.id);
res.json(account);
});
},
init : function(myservices){
services = myservices;
}
};
Warning Info:
body-parser deprecated bodyParser: use individual json/urlencoded middlewares app.js:8:9
body-parser deprecated undefined extended: provide extended option node_modules/body-parser/index.js:105:29
Solution:
Change the body parser part as follow:
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
References:
should.js provide the test assertion
http://unitjs.com/guide/should-js.html
underscore enhance the JS operation
http://underscorejs.org/#filter
online JSON editor to validate the response
http://www.jsoneditoronline.org/
older blog about RESTful
http://sillycat.iteye.com/blog/2067546
http://sillycat.iteye.com/blog/2155801
http://sillycat.iteye.com/blog/2072384
https://github.com/luohuazju/easy/tree/master/easynodejs/buglist
发表评论
-
Stop Update Here
2020-04-28 09:00 322I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 484NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 374Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 375Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 345Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 436Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 444Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 381Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 463VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 394Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 487NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 432Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 342Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 255GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 455GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 332GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 324Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 302Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 315Serverless with NodeJS and Tenc ...
相关推荐
通过Express,开发者可以快速搭建RESTful API服务,轻松处理HTTP请求和响应。它允许开发者使用模板引擎来动态生成HTML页面,同时还支持静态文件服务,使得构建Web应用变得更加高效。 在使用这个压缩包时,首先需要...
《基于Express、MongoDB、Node.js与Gulp的图书管理系统》 在当今信息化时代,图书管理系统的构建变得越来越重要,它能有效地整理、检索和管理大量图书信息。本项目采用的技术栈是Express、MongoDB、Node.js以及Gulp...
这个项目的核心特点包括 ** CoffeeScript **、** Less **、** Node.js **、** Express **、** Gulp ** 和 ** Bower **。下面我们将逐一探讨这些技术及其在 todolistOnline 中的作用。 1. **CoffeeScript**:...
通过Express,我们可以快速搭建起RESTful API,方便前端进行数据交互。例如,设置路由来处理HTTP请求,使用res.json()返回JSON格式的数据,或者使用req.body解析POST请求中的表单数据。 MySQL作为关系型数据库管理...
在NodeJS扩展合集中,可能还包括其他的实用工具和库,比如日志管理工具 Winston、错误处理中间件 Express-Error、自动化任务工具 Gulp 或 Webpack,以及测试框架 Mocha 和 Chai 等。这些工具和框架能够帮助提升开发...
设置Gulp 概括 获取数据 介绍 实施HTTP Get 连接到MongoDB和Mongoose 使用查询字符串过滤 获取单个物品 概括 过帐数据 介绍 使用人体解析器 与邮递员进行测试 保存数据 代码清理 注入我们的模型 概括 更新数据 ...
Node.js的Express框架常用于构建RESTful API,但在这个项目中,由于使用WebSocket,路由可能更多地涉及消息的分发和处理。 4. **视图和模板引擎**:`views`目录通常包含项目的HTML模板,可能会用到EJS、Pug或Jade等...
它由一个用于使用express和mongoose托管RESTful api的nodejs后端服务器,以及一个带有ui-router的前端AngularJS应用程序组成。 Gulp用作构建管理系统,而浏览器同步用于提供静态文件。 安装 获取来源: git clone ...
在黑马电商后台管理系统中,Node.js被用作后端开发语言,通过Express或Koa等框架搭建RESTful API,处理HTTP请求,与数据库进行数据交互。开发者可以从中学习如何设置路由、中间件,以及如何使用MongoDB或MySQL等...
这是一个基于JavaScript的REST API开发模板,使用了Node.js作为后端服务器环境,Gulp作为构建工具,MongoDB作为数据库存储,Mongoose作为操作MongoDB的ODM(对象数据模型),Sinon用于模拟函数,Mocha作为测试框架,...
- Express:快速、开放、极简的Web开发框架,简化了NodeJS的Web应用开发。 - Mocha/Chai:单元测试框架,用于编写和执行测试用例。 - Gulp/Grunt:自动化工具,可以帮助完成编译、压缩、部署等任务。 - PM2:...
该项目是使用Node.js,Express和DynamoDB构建RESTful API的样板。 使用Claudia.js以无服务器方式部署到AWS Lambda和AWS API Gateway,并使用gulp部署到开发本地 建筑 AWS Lambda中提供了简单的REST API,可将数据...
- **Web 应用开发**:使用 Express 框架可以快速搭建服务器端应用,支持 RESTful API 设计。 - **实时通信**:WebSocket 协议使得 Node.js 很适合构建实时聊天、协作编辑等应用。 - **CLI 工具**:编写命令行工具,...
1. RESTful API服务器:利用Node.js和Express构建一个处理HTTP请求的API服务。 2. 实时通讯应用:结合WebSocket实现聊天室或实时通知功能。 3. 文件服务器:通过Node.js提供文件下载和上传服务。 4. 数据库操作:...
【标题】:“我的投资组合网站是使用ReactJS,gulp,webpack,Nodejs创建的” 【描述】:这个项目是一个个人投资组合网站,采用了一系列现代前端和后端技术进行构建。ReactJS作为主要的UI库,提供了组件化开发的...
这个压缩包文件“基于java的-145-nodejs电影交流网站--LW-源码.zip”显然包含了一个完整的电影交流网站的源代码,可能是为了毕业设计或课程设计项目而创建的。它融合了Java和Node.js两种技术,分别在前后端发挥重要...
### 面试题精选:MVC框架和NodeJS #### 1. Node.js简介及其应用场景 - **定义**: Node.js 是一种开源服务器环境,它基于谷歌Chrome V8 JavaScript引擎构建,使得开发人员能够在服务器端使用JavaScript进行编程。 -...
5. **RESTful API的普及**:Node.js推动了面向服务的架构(SOA)和RESTful API的广泛应用。后端提供清晰的API接口,前端根据需要调用,实现了前后端的解耦,使两者可以独立开发和迭代,提高了开发灵活性和应用可维护...
- Express.js:是最受欢迎的Node.js Web应用框架,简化了路由和中间件的使用。 - MongoDB和Mongoose:MongoDB是一个流行的NoSQL数据库,Mongoose是其在Node.js中的ODM(对象数据模型)库。 - React.js和Angular....