I am trying to most optimally structure the files of my Express app. And the Router module confuses me.
As far as I know, all queries should be in models folder.
This is my current desired setup (which does not work for my query.js and query2.js files, the error I get is that "App" is not defined).
When I put the query code (from query.js file) inside my App.js file directly, then it works however.
Also the setup I have works correctly for my Email routing.
How to fix?
Here my project structure:
project_structure
Here content of my files:
APP.JS
const express = require('express');
const request = require('request');
const requestPromise = require('request-promise');
const bluebird = require('bluebird');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
const router = require('./routes');
app.use(router);
function App(req, res) {
if (!req.url) {
req.url = '/';
req.path = '/';
}
return app(req, res);
}
module.exports.my_server = App;
INDEX.JS (in routes folder)
const express = require('express');
const router = express.Router();
const config = require('../config');
let email = require('../models/email');
const query = require("../models/query");
const query2 = require("../models/query2");
router.post('/api/subscribe', function (req, res) {
const data = {
subscribed: true,
address: req.body.email,
};
...
module.exports = router;
QUERY.JS (in models folder)
app.get('/query', function(req, res, next) {
request({
uri: 'https://queryapi.api/search?q=stuff&api-key=api_key',
qs: {
api_key: 'api_key',
}
}).pipe(res);
});
You don't have access to the variable app from your query.js.
You would want to export your methods from query.js instead so you can import them to your router like this:
EDIT: change http verb from .get to .post
replace:
app.get('/query', function(req, res, next) {
with
exports.post = function(req, res, next) {
and this is how you would assign the exported method query.post to route /api/subscribe/query
const query = require("./query");
router.post('/api/subscribe/query', query.post);
/* more end points below: */
// router.get(...
// router.put(...
// ...and so on
POST request on /api/subscribe/query
P.S: This works but things can quickly get ugly if you have more routes so it would be more maintainable if you restructure your source such that the main routes are expressed clearly in your app entry file; But that's another concern.
In index.js add this line.
router.use(query)
Related
Given a simple Express app
const express = require('express')
const app = express()
app.get('/users/:userId', (req, res) => {
console.log(req.orginalUrl) // /users/1
console.log(req.route.path) // /users/:userId
res.json({})
})
It gives me the right and complete route schema req.route.path.
But if we use a router to abstract the first part of the URI then it doesn't give the full route schema anymore.
const express = require('express')
const app = express()
const users = express.Router()
.get(':userId', (req, res) => {
console.log(req.orginalUrl) // /users/1
console.log(req.route.path) // /:userId
res.json({})
})
app.use('/users', users)
It there a way to get /users/:userId while using routers?
You just have to concatenate baseUrl and path like this :
let fullPath = req.baseUrl + req.route.path;
I want to broke down my routers into separate file rather than keeping all API routes in a single file. So i tried to identify user URL using a middle ware and call the api according to the url as you seeing bellow but middleware function is not working . how to solve this?
//HERE IS INDEX.jS file CODE
const express = require("express");
const dotenv = require("dotenv");
const app = express();
const PORT = process.env.PORT || 7000;
app.listen(PORT);
app.use("/users", require("./routes/users/users"));
//==========================================================
//HERE IS users.js FILE CODE
const express = require("express");`enter code here`
const router = require("express").Router();
express().use(selectApi);
function selectApi(req, res, next) {
console.log("this line also not executing")
switch(req.originalUrl){
case '/':
// calling api here from a nother separate file
case '/user/:id'
// calling api here from a nother separate file
}
}
module.exports = router;
There is an example in the Express.js Routing Guide. I've modified it slightly to fit your example.
users.js
var express = require('express')
var router = express.Router()
// middleware 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) {
// calling api here from a nother separate file
})
router.get('/user/:id', function (req, res) {
// calling api here from a nother separate file
})
module.exports = router
index.js
const express = require("express");
const dotenv = require("dotenv");
const app = express();
const PORT = process.env.PORT || 7000;
app.listen(PORT);
app.use("/users", require("./routes/users/users"));
I'm doing to some refactoring in my application and I don't understand why this case is failing.
index.js
const express = require('express');
const router = express.Router();
const models = require('../models');
const listRoute = require('./list');
router.use('/list', listRoute);
list.js
const express = require('express');
const router = express.Router();
const models = require('../models');
const sendListDataToView = (req, res, view) => {
// get data from backend and pass to template
}
router.route('/list/show/:id')
.get((req, res) => {
sendListDataToView(req, res, 'view-list')
})
router.route('/list/expand-records-forms/:id')
.get((req, res) => {
sendListDataToView(req, res, 'edit-list-records')
})
module.exports = router;
Trying to navigate to /list/show/3 throws a 404 error. However, if I move the definitions of these routes and sendListDataToView to index.js, the page loads fine. Is this because of the multiple router.routes?
I'm just going to wipe my original answer.
You need to create the app.
const express = require('express');
const app = express();
const models = require('../models');
const listRoute = require('./list');
app.use('/list', listRoute); // Using express now.
app.listen(8080); // starts server
For example :
server.js file
var express = require('express'),
app = express(),
port = 3000,
routes = require('./app/routes/apiRoutes');
routes(app);
app.listen(port);
routes.js file
'use strict';
module.exports = function( app ) {
var api= require('../controllers/apiController');
app.route('/get').get(api.get);
};
apiController.js file
'use strict';
exports.get = function(req, res) {
// console.log( req.app ); // access it but it didn't work ?
// here want to access app to set cookie and changed cookie ?
};
if there is another way please help me thanks :)
If I correct understand your question, with routes you can do something like this:
In routes.js file:
var router = require('express').Router()
router.get('/home', function(req, res, next) {
res.render('index')
})
module.exports = router
In server.js file:
var mainRoutes = require('./routes.js')
app.use(mainRoutes)
Best way (in my opinion) to use controllers from another file it's use exports.functionName notation:
In someController.js file:
exports.homePage = function(req, res) {
res.render('index')
}
So, your router will looks like this:
var router = require('express').Router()
var someController = require('./someController.js')
router.get('/home', someController.homePage)
module.exports = router
Do different way, use route in app.use
app.js:
const
express = require('express'),
app = express(),
port = 3000,
routes = require('./app/routes/apiRoutes');
app.use(routes);
app.listen(port);
apiRoutes.js:
const
router = require('express').Router(),
apiController = require('../controllers/apiController');
router.get(
'/get',
apiController.get);
module.exports = router;
Check this example: app.js , some route file
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.