Amy T McBride Amy T McBride - 3 months ago 7
Node.js Question

Express routing -- 404 error when routing to /secrets, what am I missing

I have an express app setup but I can't figure out how to make it route correctly. At the moment I have this in app.js:

var secrets = require('./routes/secrets');
var app = express();

app.use('secrets', secrets);

The following in /routes/secrets.js

var express = require('express');
var router = express.Router();

/* GET 'secrets' */
router.get('/secrets', function(req, res, next) {
res.render('secrets', { title: 'Colour Hack v1.1' });

module.exports = router;

The file /views/secrets.jade contains the following code:

extends layout

block content
h1 A Very Green Affair
p It began at a Green Party hustings. I was wearing a beige trenchcoat, set off perfectly by an exquisitely cut Fedora, tailored by the finest vegan outfitters a party councilor's allowance can buy. We locked eyes over the pay-what-you-like FoodCycle buffet and I knew right then something special was about to occur. Amelia Womack wore a pant-suit and has a really nice hair-style. Once she noticed me she approached immediately.
p "Hi Amy."
p She said, though it appeared as if the name was somehow unfamiliar to her, and she looked into my eyes with the sort of intensity you might expect of a fawn who's just single-handedly decimated a pack of ravaging wolves and has rounded on the ring-leader who has unexpectedly turned out to be another fawn with particularly big teeth and wolf-like fur who had managed to convince the wolves it was one of them.
p "It's strange I should run into you as I was just thinking about that... policy suggestion you made."
p Amelia said the word 'that' with an odd intonation, so it seemed almost as if she were thinking about something other than the policy document. Something secret.
p "The cat policy," I replied, my words sounding both artful and powerful as I spoke them, and considered her choice of blusher, which seemed a particularly deep red, so that it underscored her already stunning features with a power and authority that lost nothing of femininity. Amelia noticed a kind of damp feeling and wondered if she should have brought a change of underwear but she carried on in a business-like, official sort of way which also seemed to tred a strangely fine line between school-girlish and teacherly.
p "Yes, Amy, the cat policy. We really need to talk about the cats, don't we..."

But at the moment I'm just getting a 404 error when I type /secrets into the browser, after the root URL. Can anyone tell me how I can approach this differently so I register a successful GET request and properly serve the page?


First add a '/' to your request url in app.js: app.use('/secrets', secrets)

Now you still define to use "/secrets/secrets" as GET, because the setting of the Router appends this request url from app.js.

Change the request url for the Router to only / like

router.get('/', function(req, res, next) {} );