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);
Related
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;
This is my app.js
var express = require('express');
...
var model = require('./models/my_model');
var routes = require('./routes/index');
var app = express();
app.use('/', routes);
var middlewareBefore = function(req, res, next){
my_model.getName(function(err, result){
if(err){
console.log(err);
res.redirect('/error');
}else{
console.log(result);
next();
}
})
}
app.use(middlewareBefore);
...
module.exports = app;
and this is my routes/index.js file
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('login', { url: my_model.getUrl() });
});
module.exports = router();
I'm trying to access my_model from the route file. I've already tried using app.locals, res, req, res.locals and app.use('/', routes)(my_model), as seen on different questions from this page, but none of them seem to work... I wonder if the usage of express.Router() is creating this issue (note that setting the middleware before the route didn't solve this)
Several ways to do that:
1.use app.use('/', routes); after declaration of middlewareBefore variable.
2.or declare my_model directly in routes/index.js at top var model = require('./models/my_model');
and use within router.get().
3.or better use MVC architecture.
I am learning NodeJS. I have a nodeJS API project. I want to use version in routes so i created following folder structure.
application/
--app.js
--routes/
----V1/
------routes.js
------users.js
Here is my app.js file:
var v1 = require('./routes/v1/route');
app.use('/api/v1', v1.router);
And this is route.js file:
var express = require('express');
var router = express.Router();
router.get('/',function(req,res){
res.send("Welcome to Node JS V1");
});
var courses = require('./users').router;
-- How to include usres route here
module.exports.router = router;
users.js file:
var express = require('express');
var router = express.Router();
router.get('/',function(req,res){
res.send("Get all users.");
});
module.exports.router = router;
Now how i can call users from route.js file.
I'd do it a bit different than #Shaharyar
routes.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('Welcome to Node JS V1');
});
router.use('/users', require('./users').router);
module.exports.router = router;
users.js
var express = require('express');
var router = express.Router();
router.get('/',function(req, res){
res.send('Get all users.');
});
router.post('/', function(req, res) {
// Create user
res.send('Some response.');
});
module.exports.router = router;
To simplify it I'd drop the .router on module.exports and do.
router.use('/users', require('./users'));
Also the same for the route.js file, then in your server.js or wherever:
router.use('/api/v1', require('./routes'));
router.use('/api/v2', require('./routes2'));
You should create express instance once and pass it into all modules.
route.js file
//initialize
var express = require('express');
var router = express.Router();
router.get('/', function(req, res){
res.send("Welcome to Node JS V1");
});
//modules
require('./users')(router);
require('./sessions')(router); //added for explaination
require('./comments')(router); //added for explaination
//export
module.exports.router = router;
module file (any module users, comments etc)
//initialize
module.exports = function (router) {
router.get('/users', users);
}
//APIs
function users(req, res) {
res.send("Get all users.");
}
New to Node/Express, trying to figure out what the best way to make my server controllers organized and modular. I have app.js file with the following code.
var express = require('express');
var app = express();
// Launch Server
const PORT = process.env.PORT || 8888;
app.listen(PORT, function() {
console.log('Server running on port ' + PORT + '!');
});
// Render Static Views from the Public Directory/Views
app.use('/', express.static(__dirname + '/public/views/'));
// API
// var foo = require(bar);
I want to keep all of my API logic in apiController.js then require it into my app.js. What's the best way to do this?
Example GET Endpoint
app.get('/api', function(req, res) {
res.json({
success: true,
message: 'Hello World!'
});
});
Here is my project structure:
I like David Fang's solution, but I would go one step further and create an Express 4 router for your API routes. They're very composable and modular, as they can act as both routers and middleware, can be nested and imported neatly.
app.js
var apiRouter = require('./controllers/apiController.js');
app.use('/api', apiRouter);
apiController.js
var express = require('express');
var apiRouter = express.Router();
apirouter.get('/some/route', (req, res) => {
// some code...
})
module.exports = apiRouter;
Documentation: http://expressjs.com/en/api.html#router
Here's a simple method:
app.js
require('./controllers/apiController.js')(app);
apiController.js
module.exports = function(app) {
app.get('/api/some/route', function(req, res) {
// some code...
}
}
Maybe this is not the best approach, but I have used it without problem in small apps.
This is how i do it
I have app/controllers/index which call register method each of the controller. Each controller has a register method that register its route with the app.
In controllers:
exports.register = function(app) {
app.post('/login', login)
}
In controllers index.js or routes or whatever you want to name it.
require('../controllers/auth').register(app)
require('../controllers/registration').register(app)
and at the end, drops all other routes to 404.
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) => {
});
}