Monday, 23 March 2020

Newbie help: ReferenceError: req is not defined

Hi guys!​I am not sure how to get this solved, I have gone through Stackoverflow and some other resources, most of them point to this missing:var express = require('express');var app = express();But as you can see in the code it is already there.Please help me on how to identify the error and how to solve it, thanks in advance.​CODE:var express = require('express');var app = express();var bodyParser = require('body-parser');var path = require('path');// View Engineapp.set('view engine', 'ejs');app.set('views', path.join(__dirname, 'views'));// Body Parser Middlewareapp.use(bodyParser.json());app.use(bodyParser.urlencoded({extended: false}));// Set Static Pathapp.use(express.static(path.join(__dirname, 'public')));var users = [  {id: 1,first_name: 'John',last_name: 'Doe',email: 'johndoe@gmail.com',  },  {id: 2,first_name: 'Bob',last_name: 'Ross',email: 'bobross@gmail.com',  },  {id: 3,first_name: 'Max',last_name: 'Emillian',email: 'maxemillian@gmail.com',  }]app.get('/', function (req, res) {res.render('index', {title: 'Customers',users: users  });});app.post('/users/add', function(){console.log(req.body.first_name);});app.listen(3000, function () {console.log('Server started on port 3000!');});​​Error I'm getting:ReferenceError: req is not defined   at C:\Projects\test\app.js:81:15   at Layer.handle [as handle_request] (C:\Projects\test\node_modules\express\lib\router\layer.js:95:5)   at next (C:\Projects\test\node_modules\express\lib\router\route.js:137:13)   at Route.dispatch (C:\Projects\test\node_modules\express\lib\router\route.js:112:3)   at Layer.handle [as handle_request] (C:\Projects\test\node_modules\express\lib\router\layer.js:95:5)   at C:\Projects\test\node_modules\express\lib\router\index.js:281:22   at Function.process_params (C:\Projects\test\node_modules\express\lib\router\index.js:335:12)   at next (C:\Projects\test\node_modules\express\lib\router\index.js:275:10)   at serveStatic (C:\Projects\test\node_modules\serve-static\index.js:75:16)   at Layer.handle [as handle_request] (C:\Projects\test\node_modules\express\lib\router\layer.js:95:5)

Submitted March 23, 2020 at 05:20PM by DhaiKhan

No comments:

Post a Comment