Skip to content

Instantly share code, notes, and snippets.

@srinivas69
Created June 18, 2020 16:34
Show Gist options
  • Save srinivas69/d46ee6d64da84d28be503471edf7c538 to your computer and use it in GitHub Desktop.
Save srinivas69/d46ee6d64da84d28be503471edf7c538 to your computer and use it in GitHub Desktop.
Expressjs & EJS Tutorial to Choose and Upload File-Nodejs complete code
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const formidable = require('formidable');
const fs = require('fs');
const app = express();
const port = 3000;
// configure middleware
app.set('port', process.env.port || port); // set express to use this port
app.set('views', __dirname + '/views'); // set express to look in this folder to render our view
app.set('view engine', 'ejs'); // configure template engine
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // parse form data client
app.use(express.static(path.join(__dirname, 'public'))); // configure express to use public folder
// set the app to listen on the port
//upload file api
app.post('/uploadfile',upload_file);
app.get('/', open_index_page);//call for main index page
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
function upload_file(req, res, next){
if(req.method == "POST") {
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = true;
// store all uploads in the /uploads directory
form.uploadDir = path.basename(path.dirname('/uploads/json_files/'))
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, file.name), function(err){
if (err) throw err;
//console.log('renamed complete: '+file.name);
const file_path = '/uploads/'+file.name
});
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// once all the files have been uploaded, send a response to the client
form.on('end', function() {
//res.end('success');
res.statusMessage = "Process cashabck initiated";
res.statusCode = 200;
res.redirect('/')
res.end()
});
// parse the incoming request containing the form data
form.parse(req);
}
}
function open_index_page(req, res, next){
if(req.method == "GET"){
res.render('index.ejs');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment