Pranshu Pranshu - 25 days ago 8
Node.js Question

Express Framework error

I am very new to Node.js and learning about Express framework now a days.
But I am again stuck at an error which is so much irritating.
I am learning from a video tutorial in which I was learning how to use Template Engine 'EJS' and was making a very simple program peacefully until I met this Error.

TypeError: Cannot read property '_locals' of undefined
at EventEmitter.render
(G:\Docs\Node.js\node_modules\express\lib\application.js:549:11)
at G:\Docs\Node.js\app.js:6:9
at Layer.handle [as handle_request]
(G:\Docs\Node.js\node_modules\express\lib\router\layer.js:95:5)
at next (G:\Docs\Node.js\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch
(G:\Docs\Node.js\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request]
(G:\Docs\Node.js\node_modules\express\lib\router\layer.js:95:5)
at G:\Docs\Node.js\node_modules\express\lib\router\index.js:281:22
at Function.process_params
(G:\Docs\Node.js\node_modules\express\lib\router\index.js:335:12)
at next (G:\Docs\Node.js\node_modules\express\lib\router\index.js:275:10)
at expressInit
(G:\Docs\Node.js\node_modules\express\lib\middleware\init.js:40:5)


And my code is this :-

var express = require('express');

var app = express();
app.set('view engine', 'ejs');
app.set('views', __dirname, '/templates');
app.get('/', function(req, res) {
app.send("Welcome to the Homepage");
});
app.get('/profile/:name', function(req, res) {
app.render('profile');
});
app.listen(4242);
console.log("Server is running ....");


I have created a profile.ejs file in template folder which is present in my current directory and I want that when I type 127.0.0.1/4242/profile/anyname
That profile page must be rendered and shown.

my Profile.ejs page is :

<!DOCTYPE html>
<html>

<head>
<title>Profile</title>
</head>

<body>
<h1>Welcome to the Profile !!</h1>
</body>

</html>


I can't find an exact answer elsewhere.

Answer Source

Try like this,

app.get('/profile/:name', function(req, res) {
   res.render('profile'); // see the change here for *res*
});