Slip5:Create a Node.js file that writes an HTML form, with an upload field.
const http = require('http');
http.createServer(function (req, res) {
// When form is submitted
if (req.url === '/upload' && req.method === 'POST') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end("<h2>File uploaded successfully!</h2>");
}
else {
// Show form
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<h2>Upload File</h2>
<form action="/upload" method="post" enctype="multipart/form-data">
Select File:<br><br>
<input type="file" name="fileupload"><br><br>
<input type="submit" value="Upload">
</form>
`);
}
}).listen(3000);
console.log("Server running at http://localhost:3000/");
Comments
Post a Comment