55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
var express = require("express");
|
|
var bodyParser = require("body-parser");
|
|
var multer = require("multer");
|
|
var app = express();
|
|
var cors = require('cors')
|
|
app.use(cors());
|
|
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.use(function(req, res, next) {
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
next();
|
|
});
|
|
|
|
app.post("/upload", multer({dest: "/home/argirok/projects/openaire/uoa-services-portal/trunk/services/upload/uploads/"}).array("uploads[]", 12), function(req, res) {
|
|
var filepath ="/"+req.files[0].path;
|
|
console.log(req.files[0]);
|
|
if (req.files[0].mimetype != 'text/csv') {
|
|
console.error("No proper file type");
|
|
res.status(500).send(getResponse(500,"No proper file type")) ;
|
|
}else{
|
|
//send file
|
|
res.sendFile(filepath);
|
|
}
|
|
var fs = require('fs');
|
|
fs.stat(filepath, function (err, stats) {
|
|
console.log(stats);//here we got all information of file in stats variable
|
|
|
|
if (err) {
|
|
return console.error(err);
|
|
}
|
|
|
|
fs.unlink(filepath,function(err){
|
|
if(err) return console.log(err);
|
|
console.log('file deleted successfully');
|
|
});
|
|
});
|
|
|
|
});
|
|
app.get('/get/d6bd36a06762e2f0da891f9670fcb2b0', function(req, res) {
|
|
res.sendFile('uploads/d6bd36a06762e2f0da891f9670fcb2b0')
|
|
})
|
|
var server = app.listen(8000, function() {
|
|
console.log("Listening on port %s...", server.address().port);
|
|
});
|
|
function getResponse(code, message){
|
|
var response ={};
|
|
response["code"]=code;
|
|
response["message"]=message;
|
|
return response;
|
|
|
|
}
|