node.js - Using routes in Express-js -
node.js - Using routes in Express-js -
so i'm starting utilize node.js. saw video ryan dahl on nodejs.org , heard recommended express-js websites.
i downloaded latest version of express, , began code. have fledged static view on /, seek sending parameters, errors this:
cannot /wiki
i tried next guide on expressjs.com way 1 uses routes has changed in latest version, makes guide unusable.
guide:
app.get('/users/:id?', function(req, res, next){ var id = req.params.id; if (id) { // } else { next(); } });
generated express:
app.get('/', routes.index);
my problem arises when seek , add together route.
app.get('/wiki', routes.wiki_show);
i've tried bunch of approaches, maintain getting cannot /wiki
(404) error.
routes/index.js looks this:
exports.index = function(req, res) { res.render('index', { title: 'test', articles: articles, current_article: current_article, sections: sections }) };
the thing did there add together parameters (arrays in same file) , working. when re-create contents , alter exports.index
exports.wiki
or exports.wiki_show
still cannot /wiki
error.
can explain me i'm missing here? - thanks.
so, after created question, got related list on right similar issue: organize routes in node.js.
the reply in post linked express repo on github , suggests @ 'route-separation' example.
this helped me alter code, , have working. - comments.
my implementation ended looking this;
i require routes in app.js:
var express = require('express') , site = require('./site') , wiki = require('./wiki');
and add together routes this:
app.get('/', site.index); app.get('/wiki/:id', wiki.show); app.get('/wiki/:id/edit', wiki.edit);
i have 2 files called wiki.js , site.js in root of app, containing this:
exports.edit = function(req, res) { var wiki_entry = req.params.id; res.render('wiki/edit', { title: 'editing wiki', wiki: wiki_entry }) }
node.js url-routing express
Comments
Post a Comment