I'm trying to use body-parser to just log a number I type in an input.
My HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
</head>
<body>
<form action="fileupload" method="post" enctype="multipart/form-data">
<input type="file" name="filetoupload"><br>
<lable>What Grade</lable><input type="text" name="gradeNumber"><br>
<input type="submit">
</form>
</body>
</html>
Heres my Node JS code:
const express = require('express');
const formidable = require('formidable');
const fs = require('fs');
const path = require('path');
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.get('/fileupload', function (req, res) {
res.render("upload")
});
app.post('/fileupload', function (req, res) {
console.log(req.body.gradeNumber);
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
var newpath = 'C:/Users/Shubh Computer/Desktop/VSCode/Grades/1/' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
});
});
app.listen(3000);
When I run the project I get the value "undefined". I know I've probably done a bad mistake since I'm a beginner. If anybody could help that would be great!