How to separate routes on Node.js and Express 4? - node.js

I want to separate Routes from my server.js file.
I am following this tutorial on Scotch.io
http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4
It is working if all lines are on server.js file. But I am failing to separate. How can I make this work?
server.js
// set up ======================================================================
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// configuration ===============================================================
app.use(bodyParser());
var port = process.env.PORT || 8000;
var mongoose = require('mongoose');
var database = require('./config/database');
mongoose.connect(database.url);
var Video = require('./app/models/video');
// routes =======================================================================
app.use('/api', require('./app/routes/routes').router);
// listen (start app with node server.js) ======================================
app.listen(port);
console.log("ready captain, on deck" + port);
module.exports = app;
And the app/routes/routes.js
var express = require('express');
var router = express.Router();
router.use(function(req, res, next) {
console.log('Something is happening.');
next();
});
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our rest video api!' });
});
router.route('/videos')
.post(function(req, res) {
var video = new Video();
video.title = req.body.title;
video.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Video criado!' });
});
})
.get(function(req, res) {
Video.find(function(err, videos) {
if (err)
res.send(err);
res.json(videos);
});
});
module.exports.router = router;

Server.js
var express = require('express');
var app = express();
app.use(express.static('public'));
//Routes
app.use(require('./routes')); //http://127.0.0.1:8000/ http://127.0.0.1:8000/about
//app.use("/user",require('./routes')); //http://127.0.0.1:8000/user http://127.0.0.1:8000/user/about
var server = app.listen(8000, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
routes.js
var express = require('express');
var router = express.Router();
//Middle ware that is specific to this router
router.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
// Define the home page route
router.get('/', function(req, res) {
res.send('home page');
});
// Define the about route
router.get('/about', function(req, res) {
res.send('About us');
});
module.exports = router;
*In routs.js you should define Middle ware
ref http://wiki.workassis.com/nodejs-express-separate-routes/

As far as separating routes from main file is concerned..
Server.js
//include the routes file
var routes = require('./routes/route');
var users = require('./routes/users');
var someapi = require('./routes/1/someapi');
////////
app.use('/', routes);
app.use('/users', users);
app.use('/1/someapi', someapi);
routes/route.js
//last line - try this
module.exports = router;
Also for new project you can try on command line
express project_name
You will need express-generator for that

Another way to separate routes into their own files with Express 4.0:
server.js
var routes = require('./routes/routes');
app.use('/', routes);
routes.js
module.exports = (function() {
'use strict';
var router = require('express').Router();
router.get('/', function(req, res) {
res.json({'foo':'bar'});
});
return router;
})();

One way to separate routes into their own file.
SERVER.JS
var routes = require('./app/routes/routes'); //module you want to include
var app=express();
routes(app); //routes shall use Express
ROUTES.JS
module.exports=function(app) {
//place your routes in here..
app.post('/api/..., function(req, res) {.....} //example
}

If you're using express-4.x with TypeScript and ES6, this would be a best template to use;
src/api/login.ts
import express, { Router, Request, Response } from "express";
const router: Router = express.Router();
// POST /user/signin
router.post('/signin', async (req: Request, res: Response) => {
try {
res.send('OK');
} catch (e) {
res.status(500).send(e.toString());
}
});
export default router;
src/app.ts
import express, { Request, Response } from "express";
import compression from "compression"; // compresses requests
import expressValidator from "express-validator";
import bodyParser from "body-parser";
import login from './api/login';
const app = express();
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
app.get('/public/hc', (req: Request, res: Response) => {
res.send('OK');
});
app.use('/user', login);
app.listen(8080, () => {
console.log("Press CTRL-C to stop\n");
});
Much clear and reliable rather using var and module.exports.

We Ought To Only Need 2 Lines of Code
TL;DR
$ npm install express-routemagic --save
const magic = require('express-routemagic')
magic.use(app, __dirname, '[your route directory]')
That's it!
More info:
How you would do this? Let's start with file structuring:
project_folder
|--- routes
| |--- api
| |--- videos
| | |--- index.js
| |
| |--- index.js
|
|--- server.js
Note that under routes there is a structure. Route Magic is folder aware, and will imply this to be the api uri structure for you automatically.
In server.js
Just 2 lines of code:
const magic = require('express-routemagic')
magic.use(app, __dirname, 'routes')
In routes/api/index.js
const router = require('express').Router()
router.get('/', (req, res) => {
res.json({ message: 'hooray! welcome to our rest video api!' })
})
In routes/api/videos/index.js
Route Magic is aware of your folder structure and sets up the same structuring for your api, so this url will be api/videos
const router = require('express').Router()
router.post('/', (req, res) => { /* post the video */ })
router.get('/', (req, res) => { /* get the video */ })
Disclaimer: I wrote the package. But really it's long-overdue, it reached my limit to wait for someone to write it.

An issue I was running into was attempting to log the path with the methods when using router.use ended up using this method to resolve it. Allows you to keep path to a lower router level at the higher level.
routes.js
var express = require('express');
var router = express.Router();
var posts = require('./posts');
router.use(posts('/posts'));
module.exports = router;
posts.js
var express = require('express');
var router = express.Router();
let routeBuilder = path => {
router.get(`${path}`, (req, res) => {
res.send(`${path} is the path to posts`);
});
return router
}
module.exports = routeBuilder;
If you log the router stack you can actually see the paths and methods

I simply delcared the files and used require in the server.js file
app.use(express.json());
require('./app/routes/devotion.route')(app);
require('./app/routes/user.route')(app);

In my case, I like to have as much Typescript as possible. Here is how I organized my routes with classes:
export default class AuthService {
constructor() {
}
public login(): RequestHandler {
return this.loginUserFunc;
}
private loginUserFunc(req: Request, res: Response): void {
User.findOne({ email: req.body.email }, (err: any, user: IUser) => {
if (err)
throw err;
if(!user)
return res.status(403).send(AuthService.noSuccessObject());
else
return AuthService.comparePassword(user, req, res);
})
}
}
From your server.js or where you have your server code, you can call the AuthService in the following way:
import * as express from "express";
import AuthService from "./backend/services/AuthService";
export default class ServerApp {
private authService: AuthService;
this.authService = new AuthService();
this.myExpressServer.post("/api/login", this.authService.login(), (req: express.Request, res: express.Response) => {
});
}

Related

I cannot get /users json

I'm new to express and node js, I recently learned how to write API for express but at the end, I get some sort of problem which not resolved by me after several tries. I could not get a response on localhost:8080/users
src/routes/users.js
const { Router } = require("express");
const router = Router();
const getUsers = (req, res) =>
res.send(Object.values(req.context.models.users));
const getUser = (req, res) =>
res.send(req.context.models.users[req.params.userId]);
router.get("/users/", getUsers);
router.get("/users/:userId", getUser);
module.exports = router;
src/routes/index.js
const user = require("./user");
const message = require("./message");
module.exports = {
user,
message
};
src/index.js
const express = require("express");
const app = express();
// Custom Modules
const routes = require("./routes");
const models = require("./models");
// Application-Level Middleware Starts
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
req.context = {
models,
me: models.users[1]
};
console.log(req.context.models.users);
next();
});
// Used Routes
app.use("/users", routes.user);
app.use("/messages", routes.message);
// App Listning to HTTPS Module
app.listen(process.env.PORT);
You need to fix your endpoints in users.js:
router.get("/", getUsers);
router.get("/:userId", getUser);
The reason is because of app.use("/users", routes.user); in your index.js, where the endpoint for users is set. If you leave /users/ in users.js it would be localhost:8080/users/users/. Same problem might be with /messages.

How can I separate route files?

I have a route file in my project and it is called from my app with these lines:
var index = require('./routes/index');
app.use('/', index);
But I need to separate the route file, and I'm trying to do this:
var index = require('./routes/index');
var user = require('./routes/user');
app.use('/', index);
app.use('/user', user);
In route user.js I put the service that I need to access from the client. But it's not working. I don't know what is wrong, I am a beginner in Node.js.
The request returns:
GET /user/find 304 4.203 ms - -
And user.js file is:
var router = express.Router();
router.get('/user/find',function(req, res){
Object.find(function(err, s){
if(err) res.send(err);
res.json(s);
});
});
module.exports = router;
*This request works well on index.js
You put user router under /user route, and in your user router you defined app.get('/user/find'), so the actual path would be /user/user/find, you need to remove the user prefix in router
var router = express.Router();
router.get('/find',function(req, res){
Object.find(function(err, s){
if(err) res.send(err);
res.json(s);
});
});
module.exports = router;
A simple way to do this can be:
index.js
var express = require('express')
var app = express()
var route1 = require('./route1')
var route2 = require('./route2')
app.use('/', route1);
app.use('/hello', route2);
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
route1.js
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Hello route1');
})
module.exports = router
route2.js
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('Hello route2');
})
module.exports = router
Have you made sure to include a module.exports = router at the end of each of your route files?
Your route files should be set up thusly:
var router = require('express').Router();
router.get("/example", function (req, res) {
res.send("Hello");
});
module.exports = router;

Express Middleware Issue

On routes/index.js it works fine if I leave the module.exports = routes;
But If I change it to the following to allow multiple files then I get a middleware error:
module.exports = {
routes
};
var app = express();
const routes = require('./routes');
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use('/', routes);
app.get('/', (req, res) => {
res.send('Please visit: http://domain.com');
}, (err) => {
res.send(err);
});
//routes/index.js
const routes = require('./MainRoutes');
module.exports = routes;
//routes/Main Routes.js
const routes = require('express').Router();
routes.post('/main', (res, req) => {
//code here works
});
module.exports = routes;
The error is: Router.use() requires middleware function but got a ' + gettype(fn));
MainRoutes.js exports the express router object, which middleware will understand just fine if you do
module.exports = routes; // routes/index.js
However, when you do
module.exports = {
routes
};
You are now nesting that router object in another object, which middleware can't understand.
In your main server file you can do
const {routes} = require('./routes');
to get the router object properly.
Modify the routes/index.js as:
const routes = require('express').Router();
routes.use('/main', require('./MainRoutes'));
// Put other route paths here
// eg. routes.use('/other', require('./OtherRoutes'))
module.exports = routes;
Modify the Main Routes.js as:
const routes = require('express').Router();
routes.post('/', (res, req) => {
// route controller code here
});
module.exports = routes;
Hope this helps you.

separate file for routes in express

I was wondering how do I move all of my api routes in express into a separate routes.js file from my server.js file
I have a long list of api routes using app.use() for each route. So each route is in its own file, e.g. movies.js, movie.js but when I list these it makes for a long list in server.js
So I want to remove the list of api endpoints section from the below server.js out to a routes.js file.
Here is what I have currently:
server.js
import path from 'path'
import express from 'express'
import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackConfig from './webpack.config.dev'
const app = express();
/* api endpoints, can be many more, I want them in routes.js */
app.use('/api/movies', require('./src/api/routes/movies'))
app.use('/api/movie', require('./src/api/routes/movie'))
app.use(webpackDevMiddleware(webpack(webpackConfig), {
publicPath: webpackConfig.output.publicPath
}));
app.use('/public', express.static(__dirname + '/public'))
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(3000, 'localhost', function (err) {
if (err) {
console.log(err);
return;
}
})
An example route
movies.js
var express = require('express');
var request = require("request");
var router = express.Router();
router.get('/', function(req, res) {
res.json({})
});
module.exports = router;
You can try to use the following modular approach.
Define controller files having logic per feature. e.g. movie.
movieController.js
module.exports = {
getMovie : function(req, res){
//do something
},
getMovies : function(req, res){
//do something
},
postMovie : function(req, res){
//do something
}
}
Then, reference that controller in routes files and simply plug those functions.
routes.js
var express = require('express');
var movieCtrl = require('./movieController');
var router = express.Router();
router.route('/movie').get(movieCtrl.getMovie);
router.route('/movie').post(movieCtrl.postMovie);
router.route('/movies').get(movieCtrl.getMovies);
module.exports = router;
And, in app.js, mount the routes to suitable location, e.g. /api
app.js
var routes = require('./routes');
app.use('/api', routes);

How to call express 4.0 middleware function in app.js from router?

I have a middleware function to do the basic authorization which is in app.js (starter javascript). But there are a few router files in other javascript files. My question is how I can call the authorization middleware function in my router?
Here is my app.js:
var basicAuth = require('basic-auth');
var auth = function (req, res, next) {
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.send(401);
};
var user = basicAuth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
};
if (user.name === 'foo' && user.pass === 'bar') {
return next();
} else {
return unauthorized(res);
};
};
module.exports = app;
My router file index.js:
var express = require('express');
var router = express.Router();
router.get('/', auth, function(req, res) {
res.render('index', { title: 'Express' });
});
module.exports = router;
Apparently the auth is not defined here. Can anybody tell me how can I use auth middleware function in my index.js router functions? Thanks
I'm not sure exactly what you're attempting to export with.
module.exports = app;
in app.js
Here is a simplifed solution:
index.js
var express = require('express');
var app = express();
var auth = require('./auth');
var router = express.Router();
router.use(auth); // Auth middleware is first
router.get('/', function(req, res) {
return res.send('Hello world');
}
// More routes can go here
// require('./external')(router);
// router.get('/route', function(req, res) {}
app.use(router);
app.listen(3000);
auth.js
module.exports = function(req, res, next) {
return next();
}
If you have routes in other files you can either pass app or router
external.js
module.exports = function(router) {
}
Why not put your auth middleware in a separate file that you can require() and use from both app.js and your router files?

Resources