Express: set base url with generics endpoints from very beginning

Created on 23 Dec 2016  路  3Comments  路  Source: expressjs/express

I have created server.js and route.js file for restful api development. I want to add endpoints in base url in server.js not in route.js. see this in below mentioned code

Scenario 1 What should i do right now

server.js

var express     = require('express');
var app         = express();

var routes = require("./app/routes/routes.js")(app);

-------------------------------------------------------------------------
routes.js

var appRouter = function(app) {
    app.get("/api/:id", function(req, res) {
        console.log('-------------------User detail fetched..............')
    });
    app.delete("/api/:id", function(req, res) {
        console.log('-------------------particular user detail..............')
    });
}
module.exports = appRouter;

I want to extract out api keywords end points from routes.js file and put somewhere in server.js file. So my future route.js updated code is mentioned below. how we implement generic api end points from server.js file.

routes.js

var appRouter = function(app) {
    app.get("/:id", function(req, res) {
        console.log('-------------------User detail fetched..............')
    });
    app.delete("/:id", function(req, res) {
        console.log('-------------------particular user detail..............')
    });
}
module.exports = appRouter;

Please guide me.. thanks in advance

4.x question

Most helpful comment

Structure your app like the following:

/// server.js

var express     = require('express');
var app         = express();

var routes = require("./app/routes/routes.js");

app.use("/api", routes);

/// -------------------------------------------------------------------------
/// routes.js

var express = require("express");

var appRouter = express.Router();

    appRouter.get("/:id", function(req, res) {
        console.log('-------------------User detail fetched..............')
    });
    appRouter.delete("/:id", function(req, res) {
        console.log('-------------------particular user detail..............')
    });
}
module.exports = appRouter;

All 3 comments

Hi
You can read this article especially part express.Router it down below page
I hope it's helps you
Sorry for my English

Structure your app like the following:

/// server.js

var express     = require('express');
var app         = express();

var routes = require("./app/routes/routes.js");

app.use("/api", routes);

/// -------------------------------------------------------------------------
/// routes.js

var express = require("express");

var appRouter = express.Router();

    appRouter.get("/:id", function(req, res) {
        console.log('-------------------User detail fetched..............')
    });
    appRouter.delete("/:id", function(req, res) {
        console.log('-------------------particular user detail..............')
    });
}
module.exports = appRouter;

Forget to close the issue but it's really helpful to follow the architecture while developing node.js app

Was this page helpful?
0 / 5 - 0 ratings