I am starting a new Node.js app and this time, I'm trying to organize the code correctly instead of having everything in the same file.
I only have a simple setup now at server.coffee
:
express = require 'express'
app = module.exports = express.createServer()
## CONFIGURATION ##
app.configure () ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.use express.bodyParser()
app.use express.logger('dev')
app.use express.profiler()
app.use express.methodOverride()
app.use app.router
app.use express.static(__dirname + '/public')
app.configure 'development', () ->
app.use express.errorHandler({dumpExceptions: true, showStack: true})
app.configure 'production', () ->
app.use express.errorHandler()
app.get '/', (req,res) ->
res.render 'index'
title: 'Express'
## SERVER ##
port = process.env.PORT || 3000
app.listen port, () ->
console.log "Listening on port" + port
I have some questions regarding that simple code and I know that all the answers depend on the developer but I really want to do it right:
server.js
file have more than the app.listen
? What should be there exactly?app.get
to other file and make them work when I run the server.coffee
?index.coffee
that I see in a lot of apps like Hubot?I hope someone can give me an answer other than "it depends".
Create Employee Route File in Node First, go to the server folder, create a new folder call it “routes”. Inside the routes folder, create a new file and name it “employees. js” where we specify our Employees Routes. So first we need to import a module that is “express” into the employees.
Routing with Express in Node: Express. js has an “app” object corresponding to HTTP. We define the routes by using the methods of this “app” object. This app object specifies a callback function, which is called when a request is received.
You can leverage require
, and simply pass the app
var in as a parameter to a method. It's not the prettiest syntax, nor is it in CoffeeScript, but you should get the idea.
module.exports = function (app) {
// set up the routes themselves
app.get("/", function (req, res) {
// do stuff
});
};
require("./routes")(app);
If you want to take it a step further, I separate my routes into smaller groups, and in it's own subfolder. (like: routes/auth.js
for login/logout, routes/main.js
for home/about/contact and so on)
// export a function that accepts `app` as a param
module.exports = function (app) {
require("./main")(app);
// add new lines for each other module, or use an array with a forEach
};
(rename routes.js
from before as routes/main.js
, the source itself remains the same)
Express 4 simplifies this with the express.Router
class.
The other feature to help organize routes is a new class,
express.Router
, that you can use to create modular mountable route handlers. ARouter
instance is a complete middleware and routing system; for this reason it is often referred to as a “mini-app”.The following example creates a router as a module, loads a middleware in it, defines some routes, and mounts it on a path on the main app.
Create a router file named
birds.js
in the app directory, with the following content:var express = require('express'); var router = express.Router(); // middleware 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('Birds home page'); }); // define the about route router.get('/about', function(req, res) { res.send('About birds'); }); module.exports = router;
Then, load the router module in the app:
var birds = require('./birds'); app.use('/birds', birds);
The app will now be able to handle requests to
/birds
and/birds/about
, along with calling thetimeLog
middleware specific to the route.
There are 2 similar question that can help you a lot with this:
How to structure a express.js application?
Nodejs/Expressjs app structure
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With