最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I use external routes.js so I don't have to define my routes in app.js? - Stack Overflow

programmeradmin1浏览0评论

I'd like to do a simple link in express. I put into the index.jade

a(href='/test', title='View test page') Test

created in /routes a test.js

/*
* GET test page.
*/

exports.test = function(req, res){
res.render('test', { title: 'Genious test' });
};

and a simple test.jade in /views

extends layout

block content
h1= title
p Test page

I added in app.js

app.get('/test', routes.test);

If I click on the link I get the error

Cannot GET /test

I'd like to do a simple link in express. I put into the index.jade

a(href='/test', title='View test page') Test

created in /routes a test.js

/*
* GET test page.
*/

exports.test = function(req, res){
res.render('test', { title: 'Genious test' });
};

and a simple test.jade in /views

extends layout

block content
h1= title
p Test page

I added in app.js

app.get('/test', routes.test);

If I click on the link I get the error

Cannot GET /test
Share Improve this question edited Sep 16, 2013 at 7:59 Evan Carroll 1 asked Mar 27, 2013 at 17:24 ObjectiveObjective 8681 gold badge12 silver badges27 bronze badges 5
  • Did you restart node after changing your app.js? Just a thought because everything looks correct. – Stan Cromlish Commented Mar 27, 2013 at 17:26
  • I restarted node but got an error so I had to remove app.get('/test', routes.test); however the link is still not working – Objective Commented Mar 27, 2013 at 17:33
  • Can you show more of your app.js? What was the error you got when you restarted node? – Stan Cromlish Commented Mar 27, 2013 at 17:36
  • the error was: /Applications/MAMP/htdocs/express/myapp/myapp/node_modules/express/lib/router/index.js:252 throw new Error(msg); ^ Error: .get() requires callback functions but got a [object Undefined] at /Applications/MAMP/htdocs/express/myapp/myapp/node_modules/express/lib/router/index.js:252:11 the app.js is basically just what express generated – Objective Commented Mar 27, 2013 at 17:40
  • routes.test is undefined, so please post how you actually define routes. – robertklep Commented Mar 27, 2013 at 17:49
Add a ment  | 

4 Answers 4

Reset to default 4

Ok. In your app.js add the following lines, and your problems will be solved.

var test = require('./routes/test.js')

app.get('/test', test.test);

This will definitely allow for your app to run and the link to be active.

Instead of creating routes/test.js, add this on to routes/index.js

exports.test = function(req, res){
    res.render('test', { title: 'Test' });
};

Then app.get('/test', routes.test); will work as expected.

This worked for me:

Adding

exports.newpage = function(req, res){
  res.render('newpage');
};

to /routes/index.js. Then adding

app.get('/newpage', routes.newpage);

under

app.get('/', routes.index);

at the bottom of server.js (or app.js).

Months after the fact, I'm going to answer this a different way. You simply wanted it to work and you got a solution for it to work. But, what if you really wanted another routes.js to handle more of your routes? Fear not node can handle this!

Assuming this structure,

- app.js
- routes/
  -  index.js
  -  foo.js

And, let's say you want the syntax in your app.js

app.get('/test', routes.foo.test);

For this, in routes.js add,

var foo = exports.foo = require('./foo');

Then in app.js you'll have,

var routes = require('./routes');

This will work, it'll allow you to say in app.js,

app.get('/test', routes.foo.test);

This will give you another level to organize your routes. I usually like to keep all of my routes organized under the routes object, but if you want to import directly into main you can too,

var foo = require('./routes/foo');
app.get('/test', foo.test);
发布评论

评论列表(0)

  1. 暂无评论