NodeJs implements a simple WEB upload and download server

The requirement on the project is that the cluster can generate PDF files or access PDF files, but there is no file server, so a simple file server is made.

Solution: After the machine (client) in the cluster generates the PDF file, push the PDF file to the file server, we will call it the server for the time being; if a client needs to access the PDF file, go to the server to get it (because Maybe other clients have already generated the PDF file), if not found, the client will generate the PDF file, and then push the PDF file to the server.


For simplicity of implementation, NodeJs is now used to implement a small program. Not much to say, go directly to the code...


server.js

var express = require('express');
var url = require('url');
var fs = require('fs');
var http = require('http');
var queryString = require('querystring');
var bodyParser = require('body-parser');
var path=require('path');

var app = express ();

app.use(bodyParser.json({limit:'1000kb'}));
app.use(bodyParser.urlencoded({limit:'1000kb',extended:true}));

var count=0;

app.post('/upload.node',function(req,resp){
	console.log('Upload request'+ (new Date()));
	var data = new Buffer(req.body.fileData,'base64');
	var filePath = req.body.filePath;
	var pathObj = path.parse(filePath);//Operate the file path string
	
	var responseBody = {};
	mkdirsSync(pathObj.dir);//Create a file directory recursively
	try{
		var writerStream = fs.createWriteStream(filePath);
		writerStream.write(data);	
		writerStream.end();
		
		writerStream.on('finish', function() {
			console.log('Upload completed'+ (new Date()));
		});
		writerStream.on('error', function(err){
			console.log(err.stack);
		});
		responseBody=JSON.stringify({
			returnMsg:'200'
		});
	}catch(err){
		console.log('Upload error'+ (new Date()));
		responseBody=JSON.stringify({
			returnMsg:'400'
		});
	}
	//write to file
	
	//console.log('*** ' + count +' ***');
    resp.status(200).end(responseBody.toString());
});

app.post('/download.node',function(req,resp){
	console.log('download request'+ (new Date()));
	var filePath = req.body.filePath;
	console.log('download');
	var responseBody = {};
	if(fs.existsSync(filePath)){
		var data = fs.readFileSync(filePath);
		var dataBase64 = data.toString ('base64');
		responseBody = JSON.stringify({
			returnMsg:'200',
			filePath:filePath,
			fileData:dataBase64
		});
		console.log('Download complete'+ (new Date()));
	}else{
		responseBody=JSON.stringify({
			returnMsg:'400'
		});
		console.log('File not found'+ (new Date()));
	}
	//console.log('*** ' + ++count +' ***');
	resp.status(200).end(responseBody.toString());
});

//Recursively create file directory synchronization method
function mkdirsSync(filePath){
	if(fs.existsSync(filePath)){
		return true;
	}else{
		if(mkdirsSync(path.dirname(filePath))){
			fs.mkdirSync(filePath);
			return true;
		}
	}
}


var server = app.listen(20001,function(){
	
	console.log('Server started.');
})

Provide a test js code.. The browser can trigger the upload process by requesting http://127.0.0.1:20000/upload.do?filePath=XXXX through get

var express = require('express');
var url = require('url');
var fs = require('fs');
var http = require('http');
var queryString = require('querystring');
var bodyParser = require('body-parser');

var app = express ();

app.use(bodyParser.json({limit:'1000kb'}));
app.use(bodyParser.urlencoded({limit:'1000kb',extended:true}));

var count=0;

app.get('/upload.do',function(req,resp){
	console.log('upload.do');
	//var path = url.parse(req.url).pathname;
	//console.log('Request for ' + path);
	var filePath = req.query.filePath;
	//var fileData = req.query.fileData;
	var address = req.query.address;
	
	upload(filePath,'127.0.0.1',20001);
	console.log('*** ' + ++count +' ***');
	var response = {
       "first":req.query.filePath,
       "last":req.query.address
   };
   resp.end(JSON.stringify(response));
});

function upload(filePath,address,port){
	
	var Data = readFile(filePath);
	var dataBase64 = Data.toString ('base64');
	console.log(Data);
	console.log(dataBase64);
	//var DataJSON = JSON.stringify(Data);
	
	//fs.writeFile('D:/input.txt',dataBase64,function(err){
	//	if(err){
	//		console.err(err);
	//	}
	//});
	var JsonData = queryString.stringify({
		filePath:filePath,
		fileData:dataBase64
	});
	//console.log(JsonData);
	var options = {
		method: "POST",
		host : address,
		port: port,
		path : '/upload.node',
		headers: {
			'Content-Type':'application/x-www-form-urlencoded'
		}
	};
	
  var req = http.request(options, function(res){
	  res.setEncoding('utf8');
  });
  req.write(JsonData);
  req.end();
}

function readFile(filePath){
	var fileData = '';
	try{
		fileData = fs.readFileSync(filePath);
	}catch(e){
		fileData = '';
	}
	return fileData;
}

app.post('/download.do',function(req,resp){
	//You can directly refer to the code of server.js
})

var server = app.listen(20000,function(){
	
	console.log('Server started.');
})


This is just a simple Nodejs program. Use this program to test directly. The result is that it can process more than 80 requests per second, which can basically meet the current needs. There are still many improvements, such as adding cache and queue. ....etc.

For NodeJs, I have only learned a little bit, and there are still many knowledge points that have not been explored. Just look at it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324417706&siteId=291194637