Why I getting this 'res.render() is not a function' - javascript

I am trying create a route, but I getting this error.
res.send is not a function
And my code in the index.js file it is this way
var express = require('express');
var router = express.Router();
router.get('/', function(res, req, next){
res.render('index');
});
module.exports = router;
And in the app.js file is that way
var index = require('./routes/index.js');
...
...
...
app.get('/', index);
Thank you, since already.

It looks like you've swapped req and res in your router.get callback. Thus, what you've named req is actually res, and vice versa, and req.render does not exist.
Try changing:
router.get('/', function(res, req, next){
to:
router.get('/', function(req, res, next){
To avoid mixing these up in the future, try to remember that requests come before responses, in both HTTP and the alphabet.

Related

Basic Express Routes puzzle

I got stuck in Mozilla js tutorial and need help. Here are excerpts from 3 files:
a)
the 2 pieces from app.js file show where to find Router handlers and then where to present them (my guess)
//app.js
//the following 3 vars do sit in routes folder, code copy-pasted var 3 my addition
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var coolRouter = require('./routes/cool');
var app = express();
....
//the following 2 'use' work just fine, the third sends err 404, 'not found'
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/users/cool', coolRouter);
b) part of the users.js sitting in routes folder:
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
c) part of the cool.js copied from the previous and sitting in routes folder:
/* GET cool text. */
router.get('/', function(req, res, next) {
res.send('you are cool, kid!');
});
I'd like to get it why it does not work.
The reason you cannot reach /users/cool is that your request is being cathed by the /users endpoint.
In order for your code to work as you intend it to do, you will have to place your routes like this:
app.use('/', indexRouter);
app.use('/users/cool', coolRouter);
app.use('/users', usersRouter);
With the /users/cool endpoint before the /users endpoint. That way, the request to /users/cool will not be catched by another endpoint.

Using multiple router defined in their own files in express

I am new to Node.js and express framework. I am getting an error when I am trying to go to the signuplogin route from my homepage route. I have also tried all the similar questions in the stackoveflow, but none of them have worked for me, so I thought of posting my own.
This is my app.js
var homepage = require('./routes/homepage');
var signupLogin = require('./routes/signuplogin');
var app = express();
app.use("/", homepage);
app.use("/signup-login", signupLogin);
This is my homepage.js in routes directory
var express = require('express');
var homepageRouter = express.Router();
/* GET Homepage. */
homepageRouter.get("/", function(req, res, next) {
res.render('../views/homepage/homepage.hbs');
});
module.exports = homepageRouter;
This is my signup-login.js in routes directory
var express = require('express');
var signupLoginRouter = express.Router();
/* GET Signup Login Page. */
signupLoginRouter.get("/signup-login", function(req, res, next) {
res.send('respond with a resource');
});
module.exports = signupLoginRouter;
Well my "/" route works perfectly but it says 404 when I try to access "/signup-login" route.
I also changed the route to "/signuplogin" because I thought maybe it doesn't except the "-" character in the route. But that didn't work as well. Any solution/advice?
You have done a mistake in your signup-login.js file. Correct the code with this -
/* GET Signup Login Page. */
signupLoginRouter.get("/", function(req, res, next) {
res.send('respond with a resource');
});
As per your code the actual url of signup page becomes to "/signup-login/signup-login"
You don't need to add the singup-login url path again in your page. That is already referenced in your main router.

Why is `app.get('*')` always triggered?

Consider this little app:
var express = require("express");
var app = express();
app.get("/json", function(req, res){
console.log("JSON route");
res.json({foo: "bar"});
});
app.get("/", function(req, res){
console.log("Slash route");
res.send("Hello");
});
app.get("*", function(req, res){
console.log("Star route");
res.redirect("/");
});
app.listen(3000, function(){
console.log("Listening.");
});
Whenever I go to either localhost:3000 or localhost:3000/json in my browser, I can see from my server log that the star * route is also triggered. This is still true if it is changed to app.get("/*")
Why is this? I thought res.send and res.json halted execution.
When you request something with your browser, it will automagically try to request a favicon. Since your server doesn't have a route defined for a favicon, it goes to the * route, causing this confusion.

One router is not triggered when declaring routers in different files

I would like to breakdown my routes in several files, typically something for the client routes and something for the api.
So I declare my app, then
app.use('/', clientRoutes);
app.use('/api', apiRoutes);
In clientRoutes:
module.exports = function (webapp_client_path){
router.get('/', function (req, res) {
res.sendFile(path.join(webapp_client_path, '/','index.html'));
});
return router;
};
In apiRoutes:
module.exports = function(passport){
router.post('/signup', function(req, res) {
console.log('signup!', req.body);
});
return router;
}
So the problem is that the apiRoutes is not triggered, if I put everything in the same cleintRoutes file it works. I've tried to change the root also ( app.use('/', apiRoutes); and inside the file router.post('/api/signup'...) but it didn't change anything.
I'm pretty sure I'm missing something basic here but can't find what it is yet. Thanks for your help !
I found the problem.
You need to execute the router then pass it as a middleware.
var apiRoutes = require('./routes/apiRoutes')();
app.use('/', apiRoutes);

I can't successfully chain my route handlers?

I've got a node.js/express app I'm working on and routes that look like-
app.post('/user/:who/update', isLoggedIn, userUpdate.save);
inside userUpdate.save I'm calling res.redirect or res.render (different routes but same idea). I don't like this. I'd like to keep my res.* calls in routes.js, but when I attempt this
app.post('/user/:who/update', isLoggedIn, userUpdate.save, function(req, res) {
res.redirect('/user/'+req.user.local.username);
});
with a call to next(req, res) at the end of userUpdate.save, the res.redirect function isn't called. What I think next() is, does not seem to be what it actually is. What am I missing?
The call to next() bypasses the remainder of your callbacks.
See this ref http://expressjs.com/api.html#app.post.method
Take a look at this example and alternately comment out each of the next() calls (and then both of them) and watch where the execution flows.
var express = require('express');
var app = express();
app.get('/test', function(req, res, next){
console.log('/test callback1');
next('route');
//next();
}, function(res, res, next){
console.log('/test callback2');
res.send('sent from /test/cb2')
});
app.get('/test', function(req, res){
console.log('response from /test route handler 2');
res.send('sent from /test route handler 2');
})
var server = app.listen(8911, function (s) {
console.log('Listening on port ', server.address().port);
});

Categories