I am getting a routing error from using express.js with node on an application I'm working on. I am failing to get post requests to properly be handled. here is my server.js file codeSever.js Code:var express = require('express');var app = express();app.set('view engine', 'ejs');var expressValidator = require('express-validator');app.use(expressValidator());var router = express.Router();app.use(router);var bodyParser = require('body-parser');app.use(bodyParser.urlencoded({extended: true}));app.use(bodyParser.json());var methodOverride = require('method-override');app.use(methodOverride(function (req, res) {if (req.body && typeof req.body === 'object' && '_method' in req.body) {var method = req.body._method;delete req.body._method;return method}}));var flash = require('express-flash');var cookieParser = require('cookie-parser');var session = require('express-session');app.use(cookieParser('csci3308'));app.use(session({secret: 'csci3308',resave: false,saveUninitialized: true,cookie: {maxAge: 60000}}));app.use(flash());var login = require('./routes/login');var success = require('./routes/success');var profileCreation = require('./routes/profileCreation');var calendar = require('./routes/calendar');var student = require('./routes/student');var professor = require('./routes/professor');app.use(express.static('public'));app.use('/', login);app.use('/login', login);app.use('/success', success);app.use('/profileCreation', profileCreation);app.use('/calendar', calendar);app.use('/student', student);app.use('/professor', professor);//error handlingapp.use((req, res, next) => {console.log(req.url)console.log(req.method)console.log(req.params)console.log(req.body)res.status(404).send("Sorry can't find that!")})app.use((err, req, res, next) =>{console.error(err.stack)res.status(500).send('Something broke!')})var port = 4000;app.listen(port, function () {console.log('Server running on http://localhost:' + port)});Here is my profileCreation.js code.var express = require('express');var router = express.Router();var expressValidator = require('express-validator');router.use(expressValidator());const { check } = require('express-validator/check');var db = require('../database.js');router.post('/profileCreation', function(request, response){console.log("made it to profileCreation post route")response.redirect('/login')//request.assert('email', 'email is required').notEmpty();// request.assert('pswd', 'password is required').notEmpty();// request.assert('cfmPswd', 'confirm password is required').notEmpty();// request.assert('pswd', 'password and confirm password are not the same').equals('cfmPswd');// var queryCheckEmailNotTaken = 'select (email) from users where(email = $1)';// if(db.oneOrNone(queryCheckEmailNotTaken, 'email')=== null){// //make eroor message// request.flash('error', 'Creation failed');// response.render('profileCreation');// }// else{// var isProf = document.getElementById("isProfessor").checked;// //var dbQueryAddUserString = 'Insert into users(email, pswd, isProffessor) values(request.sanitize('email'), request.sanitize('pswd'), isProf;// var dbQueryAddUserString = 'Insert into users(email, pswdID, isProffessor) values($1, $2, $3)';// db.none(dbQueryAddUserString, [email,pswd, isProf]).then(// response.render('login')// ).catch(function(error){// request.flash('error', 'Creation failed');// response.render('profileCreation');// })// }});router.get('/', function (request, response) {console.log('Made it to profile creation root route');response.render('profileCreation', {data: {}});});router.get('/profileCreation', function(request, response){console.log('Made it to profileCreation route');response.render('profileCreation');});module.exports = router;and here is my createProfile.ejs Code:
I am getting an 500 error when I try to use the post method from my ejs form. I am trying to build a calendar application and have the post method properly working on my login page. I am doing the exact same things in this profile creation methods and it is running the 500 error. Please help me I have no idea what the bug is and getting this bug fixed is crucial to the success of my project. Thank you in advance I am a novice at this stuff!!!
Submitted December 11, 2018 at 05:47AM by kona_coder
No comments:
Post a Comment