Monday 13 March 2017

Can't retrieve req.body from POST 200 HTTP header, blank

So I've been trying to figure this out for a while. I receive a 200 HTTP header, but no text. Code in question:todoapp/app.js:var express = require('express'); var app = express(); var hPage = require('./routes/index'); var path = require('path'); var fs = require('fs'); var bodyParser = require('body-parser'); var morgan = require("morgan"); app.use(express.static(path.join(__dirname, 'public'))); app.set('view engine', 'pug'); app.set('views', path.join(__dirname, 'views')); app.use(morgan('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended: true})); app.use('/', hPage); app.listen(8888, function(){ console.log('server is listening on port 8888.') }); views/index.pug:doctype html html head title todo link(href='/stylesheets/style.css' rel='stylesheet') body form(method='post' action= '/data') .add button.butt(onclick='handler.addTodo()' type='submit') |Add Todo input.entertext(type='text' name='todoTxt') .delete button.deleteTodoButt(onclick='handler.deleteTodo()') |Delete Todo input.deletetodo(type='text' placeholder='Enter todo position') div button.position(onclick='handler.toggleComplete()') |Toggle Complete input.togglecomplete(type='text' placeholder='Enter todo position') button.toggle(onclick='handler.toggleAll()') |Toggle All ul.todos script(src='/javascripts/main.js' type='text/javascript') routes/index.js:var express = require('express'); var router = express.Router(); var mysql = require('mysql'); var connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'todo' }); connection.connect(function(err){ if(!err){ console.log('DB is connected.'); } else { console.log(err); } }); router.use(function(req, res, next){ console.log(req.method, req.url); next(); }) router.get('/', function(req, res){ res.render('index.pug') }); router.post('/data', function(req, res){ if(!req.body.todoTxt){ console.log('Post received.'); console.log('Todo text is: ' + req.body.todoTxt); res.end(); } else{ console.log('nothing send.'); res.end(); } }); module.exports = router;

Submitted March 14, 2017 at 12:28AM by Chigurhshairdresser

No comments:

Post a Comment