Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 714
Objective Avatar asked Jan 13 '23 19:01

Objective


2 Answers

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.

like image 60
Stan Cromlish Avatar answered Jan 16 '23 20:01

Stan Cromlish


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.

like image 33
Ryan Allen Avatar answered Jan 16 '23 20:01

Ryan Allen