NodeJS RESTful Service with Gulp and Express

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326966174&siteId=291194637
Recommended