Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a new route to node express

I'm trying to add a new route in my express app but I keep getting error when trying to start the server. The error is

C:\development\node\express_app\node_modules\express\lib\router\index.js:252
    throw new Error(msg);
          ^
Error: .get() requires callback functions but got a [object Undefined]

here are my files, I'm new to node so let me know if i left out an important file

routes/furniture.js

exports.furniture = function(req, res){
   res.render('furniture', { title: '4\267pli' });
};

routes/index.js

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', { title: '4\267pli' });
};

views/furniture.ejs

<!DOCTYPE html>
<html>
<head>
    <title>4&middot;pli -- architecture</title>
    <link rel='stylesheet' href='/stylesheets/style.css'/>
    <link href='http://fonts.googleapis.com/css?family=Didact+Gothic' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="wrapper">
    <h1 class="logo"><%= title %></h1>
</div>
</body>
</html>

app.js

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , furniture = require('./routes/furniture')
  , http = require('http')
  , path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);
app.get('/furniture', routes.furniture);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
like image 803
Antarr Byrd Avatar asked May 14 '13 16:05

Antarr Byrd


People also ask

What is Express router () in node JS?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express. js.

What are routes in Express?

A route is a section of Express code that associates an HTTP verb ( GET , POST , PUT , DELETE , etc.), a URL path/pattern, and a function that is called to handle that pattern. There are several ways to create routes.

How can we create Chainable route handlers for a route path in Expressjs app?

Answer: A is the correct option. By using app. route() method, we can create chainable route handlers for a route path in Express.


1 Answers

The trouble is:

 routes = require('./routes'),
 user = require('./routes/user'),
 furniture = require('./routes/furniture'),

These 3 are setting your routes folders, not a specific file, express will look for a index.js ( not found, then --> error)

Inside these folders, you should put a index.js with your:

exports.xxxx =  function(req, res){
    res.render('xx', { foo: foo});
};

Then, your project folder structure should look like:

routes/
  ├── index.js
  │
  ├── user/
  │     └── index.js (with a exports.user inside)
  │   
  └── fourniture/
        └── index.js (with a exports.furniture inside)

You can add multiple export functions to a route like these:

app.js

// a folder called routes with the index.js file inside
routes = require('./routes')

.
.
.

app.get('/', routes.main_function);  
app.get('/sec_route', routes.sec_function);
app.post('/other_route', routes.other_function);

/routes/index.js

exports.main_function =  function(req, res){
    res.render('template1', { foo: foo });
};

exports.sec_function =  function(req, res){
    res.render('template2', { bar: bar });
};

exports.other_function =  function(req, res){
    res.render('template1', { baz: baz });
};
like image 174
jmingov Avatar answered Sep 21 '22 12:09

jmingov