I am developing a very simple JavaScript application with NodeJS. Simply upload files and view their content by paragraphs.
The problem is that every time I upload a file, I get the error "No files were uploaded." which corresponds to the following lines:
app.post('/upload_file', (req, res) => {
if (!req.files || !req.files.filetoupload) {
return res.status(400).send('No files were uploaded.');
}
[...]
}
This is the complete code:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const fs = require('fs');
const sqlite3 = require('sqlite3').verbose();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
if (!fs.existsSync('./db')) {
fs.mkdirSync('./db');
}
if (!fs.existsSync('./uploads')) {
fs.mkdirSync('./uploads');
}
let db = new sqlite3.Database('./db/paragraphs.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the paragraphs database.');
});
db.run('CREATE TABLE IF NOT EXISTS paragraphs (id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT, tag TEXT)', (err) => {
if (err) {
console.error(err.message);
}
console.log('Created paragraphs table.');
});
app.get('/', (req, res) => {
res.send('<h1>Hello World!</h1>' +
'<meta charset="utf-8">' +
'<a href="/file_uploading">Upload File</a><br>' +
'<a href="/visualize_paragraphs">Visualize Paragraphs</a><br>' +
'<a href="/tag_paragraphs">Tag Paragraphs</a>');
});
app.get('/file_uploading', (req, res) => {
res.send('<h1>File Uploading</h1>' +
'<meta charset="utf-8">' +
'<form action="/upload_file" method="post" enctype="multipart/form-data">' +
'<input type="file" name="filetoupload"><br>' +
'<input type="submit">' +
'</form>');
});
app.post('/upload_file', (req, res) => {
if (!req.files || !req.files.filetoupload) {
return res.status(400).send('No files were uploaded.');
}
let file = req.files.filetoupload;
let filename = file.name;
fs.writeFile('./uploads/' + filename, data, (err) => {
if (err) {
return console.log(err.message);
}
console.log('The file has been saved!');
});
file.mv('./uploads/' + filename, (err) => {
if (err) {
return res.status(500).send(err);
}
res.send('File uploaded!<br>' +
'<a href="/file_uploading">Upload Another File</a><br>' +
'<a href="/visualize_paragraphs">Visualize Paragraphs</a><br>' +
'<a href="/tag_paragraphs">Tag Paragraphs</a>');
});
fs.readFile('./uploads/' + filename, {encoding: 'utf-8'}, (err, data) => {
if (err) {
return console.log(err.message);
}
let lines = data.split('\n');
for (let i = 0; i < lines.length; i++) {
db.run('INSERT INTO paragraphs (text) VALUES (?)', [lines[i]], (err) => {
if (err) {
return console.log(err.message);
}
});
}
});
});
app.get('/visualize_paragraphs', (req, res) => {
db.all('SELECT * FROM paragraphs', (err, rows) => {
if (err) {
throw err;
}
rows.forEach((row) => {
res.write('<h1>Visualize Paragraphs</h1>');
res.write(row.text + '<br>');
});
res.end();
});
});
app.get('/tag_paragraphs', (req, res) => {
let paragraph_id = req.query.paragraph_id;
let tag = req.query.tag;
if (paragraph_id && tag) {
db.run('UPDATE paragraphs SET tag = ? WHERE id = ?', [tag, paragraph_id], function (err) {
if (err) {
return console.log(err.message);
}
console.log(`Row(s) updated: ${this.changes}`);
});
res.send('<h1>Tag Paragraphs</h1>' +
'<p>Paragraph ' + paragraph_id + ' has been tagged as ' + tag + '</p>');
} else {
db.all('SELECT * FROM paragraphs', (err, rows) => {
if (err) {
throw err;
}
res.write('<h1>Tag Paragraphs</h1>' +
'<table>');
rows.forEach((row) => {
res.write('<tr><td>' + row.id + '</td><td>' + row.text + '</td></tr>');
});
res.write('</table>' +
'<form action="/tag_paragraphs" method="get">' +
'<label for="paragraph_id">Paragraph ID:</label>' +
'<input type="text" id="paragraph_id" name="paragraph_id"><br>' +
'<label for="tag">Tag:</label>' +
'<input type="text" id="tag" name="tag"><br>' +
'<input type="submit" value="Submit">' +
'</form>');
res.end();
});
}
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
Can you help me? What am I doing wrong?