Taking the Express route out
Purpose
Example of how to separate route code in Express.js using npm link
.
Setting up
We can use the express generator to get up and running quickly with Express. Install it with npm install express-generator -g
if you don’t already have it:
1 | tmp$ express todo-server |
It might be worth mentioning that at the time of writing, this installs express at version approx 4.9.0 or ~4.9.0.
At this point we could npm start
to start our server. As defined in package.json, we’re basically running node ./bin/www
.
We now want to start working on a separate module which will contain the logic behind the /items route in our application:
1 | todo-server$ mkdir ../todo-items |
Writing some code
1 | todo-items$ vim index.js |
1 | var express = require('express'); |
1 | todo-items$ vim items.json |
1 | { |
We’re just defining a GET on ‘/‘. This means that, whichever route our todo-items module gets loaded on in todo-server, making a GET request on it will give us back our items.json data.
That’s great but todo-server doesn’t have todo-items installed so how’s it going to use it? We could publish todo-items on npm or host it in a Git repo on Github for example.
As you can see, we’re just experimenting with todo-items for now. Maybe later we’ll use a proper database. Maybe we’re not sure which database to go with. Point is, we don’t want to publish or host todo-items as it’s still early days, and besides, it would be better to avoid having to stay re-installing the module for every single change we make.
Enter npm link
:
1 | todo-items$ npm link |
Executing it from todo-items we get a link to this directory from our global node_modules. npm link todo-items
in todo-server gets us a link in todo-server‘s node_modules to the link in our global node_modules, effectively linking back to our todo-items directory containing our implementation for that module.
End result - if you list the contents of todo-server‘s node_modules you’ll see we have our todo-items in there, albeit a link and not an actual directory structure as our other dependencies are installed as. Now, since the todo-items installation in our todo-server is just a link, any updates we make in todo-items will automatically take effect in our todo-server project.
Seeing it work
Simply require and use:
1 | todo-server$ vim app.js |
1 | var items = require('todo-items'); |
Now we can npm start
, hit http://localhost:3000/items in a browser, and get our items.json back.