Saturday 15 April 2017

NodeJs - [Restful API] How to separate app get methods in different js files?

I am new to Node.js. Currently I am helping my team to develop a restful api with Nodejs. I would like to ask if I have to implement all the app.get methods and business logics in single file?

For example, my server.js:

var express = require('express')
var bodyParser = require('body-parser')
var app = express()
let family = {}

app.use(bodyParser.json())

app.route('/family')
  .get(function (request, response) {
    response.json(family)
  })
  .post(function (request, response) {
    family[request.body.isbn] = {
      name: request.body.name,
      relationship: request.body.relationship,
      age: request.body.age
    }

    response.json({message: 'Success'})
  })

app.listen(8080, function () {
  console.log('Server is started')
});

How if I wanna build a file call family.js and move all family related logic inside from server.js?

And how to call it from server.js?

Anybody can help? thanks.



via TechHolic

No comments:

Post a Comment