set base url with generics endpoints from very beginning

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

Author: Fantashit

1 thought on “set base url with generics endpoints from very beginning

  1. 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;

Comments are closed.