- 浏览: 2551581 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
NODEJS(3)File Upload Sample
Serving something useful
Handling POST Requests
Add a textarea in start request handler in requestHandlers.js:
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
We can add listeners in request as follow:
request.addListener("data", function(chunk){
//called when a new chunk of data was received
});
request.addListener("end", function() {
//called when all chunks of data have been received
});
Put the data and end event callbacks in the server, collecting all POST data chunks in the data callback, and calling the router upon receiving the end event, while passing the collected data chunks on to the router, which in turn passes it on to the request handlers.
Changes in server.js
function onRequest(request, response) {
var postData = "";
…snip...
request.setEncoding("utf8");
request.addListener("data", function(postDataChunk){
postData += postDataChunk;
console.log("Received POST data chunk' " + postDataChunk + "'.");
});
request.addListener("end", function() {
route(handle, pathname, response, postData);
});
…snip...
We did three things in sever.js, we defined that we expect the encoding of the received data to be UTF-8.
We add an event listener for the "data" event which step by step fills our new postData variable whenever a new chunk of POST data arrives.
When all POST data is gathered, we will call the end event to pass the data to router.
We will change the router.js and display the content.
function route(handle, pathname, response, postData) {
…snip…
handle[pathname](response, postData);
…snip…
And in requestHandlers.js, we include the data in our response of the upload request handler:
function upload(response, postData) {
…snip…
response.write("You've sent: " + postData);
…snip…
We will use querystring module to parse the postData.
var querystring = require("query string");
…snip…
querystring.parse(postData).text
…snip…
Handling file uploads
how to use an external module. The name of the external module is node-formidable. We need to install other modules in NPM.
>npm install formidable
Then we can try to use this in requestHandlers.js
var fs = require("fs");
var formidable = require("formidable");
…snip…
function start(response) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post" enctype="multipart/form-data">'+
'<input type="text" name="title" value="hello" /> <br/>'+
'<input type="file" name="upload" multiple="multiple">' +
'<input type="submit" value="Upload File" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files){
console.log("parsing done");
/* Possible error on windows system:
tried to rename to an already existing file */
fs.rename(files.upload.path, "/tmp/test.png", function(err) {
if(err){
fs.unlink("/tmp/test.png");
fs.rename(files.upload.path, "/tmp/test.png");
}
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br />");
response.write(fields.title + "<br />");
response.write("<img src='/show' />");
response.end();
});
}
function show(response){
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, file){
if(error){
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
}else {
response.writeHead(200, {"Content-Type": "image/png"});
response.write(file,"binary");
response.end();
}
});
}
exports.show = show;
In other file, router.js, server.js, index.js, we will pass the parameter request like response. And we will add show handler to our requestHandlers
route(handle, pathname, response, request);
handle[pathname](response, request);
handle["/show"] = requestHandlers.show;
references:
http://www.nodebeginner.org/#serving-something-useful
Serving something useful
Handling POST Requests
Add a textarea in start request handler in requestHandlers.js:
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
We can add listeners in request as follow:
request.addListener("data", function(chunk){
//called when a new chunk of data was received
});
request.addListener("end", function() {
//called when all chunks of data have been received
});
Put the data and end event callbacks in the server, collecting all POST data chunks in the data callback, and calling the router upon receiving the end event, while passing the collected data chunks on to the router, which in turn passes it on to the request handlers.
Changes in server.js
function onRequest(request, response) {
var postData = "";
…snip...
request.setEncoding("utf8");
request.addListener("data", function(postDataChunk){
postData += postDataChunk;
console.log("Received POST data chunk' " + postDataChunk + "'.");
});
request.addListener("end", function() {
route(handle, pathname, response, postData);
});
…snip...
We did three things in sever.js, we defined that we expect the encoding of the received data to be UTF-8.
We add an event listener for the "data" event which step by step fills our new postData variable whenever a new chunk of POST data arrives.
When all POST data is gathered, we will call the end event to pass the data to router.
We will change the router.js and display the content.
function route(handle, pathname, response, postData) {
…snip…
handle[pathname](response, postData);
…snip…
And in requestHandlers.js, we include the data in our response of the upload request handler:
function upload(response, postData) {
…snip…
response.write("You've sent: " + postData);
…snip…
We will use querystring module to parse the postData.
var querystring = require("query string");
…snip…
querystring.parse(postData).text
…snip…
Handling file uploads
how to use an external module. The name of the external module is node-formidable. We need to install other modules in NPM.
>npm install formidable
Then we can try to use this in requestHandlers.js
var fs = require("fs");
var formidable = require("formidable");
…snip…
function start(response) {
console.log("Request handler 'start' was called.");
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post" enctype="multipart/form-data">'+
'<input type="text" name="title" value="hello" /> <br/>'+
'<input type="file" name="upload" multiple="multiple">' +
'<input type="submit" value="Upload File" />'+
'</form>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files){
console.log("parsing done");
/* Possible error on windows system:
tried to rename to an already existing file */
fs.rename(files.upload.path, "/tmp/test.png", function(err) {
if(err){
fs.unlink("/tmp/test.png");
fs.rename(files.upload.path, "/tmp/test.png");
}
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br />");
response.write(fields.title + "<br />");
response.write("<img src='/show' />");
response.end();
});
}
function show(response){
console.log("Request handler 'show' was called.");
fs.readFile("/tmp/test.png", "binary", function(error, file){
if(error){
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
}else {
response.writeHead(200, {"Content-Type": "image/png"});
response.write(file,"binary");
response.end();
}
});
}
exports.show = show;
In other file, router.js, server.js, index.js, we will pass the parameter request like response. And we will add show handler to our requestHandlers
route(handle, pathname, response, request);
handle[pathname](response, request);
handle["/show"] = requestHandlers.show;
references:
http://www.nodebeginner.org/#serving-something-useful
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 475NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 336Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 384Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 422Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 247GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 293Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 573NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 265Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
nodejs-file-downloader 是一个用于下载文件的简单实用程序。 它隐藏了处理流、重定向、路径和重复文件名的复杂性。 可以自动重复失败的下载。 如果您遇到任何错误或有疑问,请不要犹豫,打开一个问题。安装$ npm ...
这个就是Vue3 + nodejs 实战 --- 文件上传项目的前端源码(Vue3)+ 后端(Nodejs),实现的文件上传的代码,主要实现了(图片上传,多文件上传,拖拽上传,大文件分片上传,断点续传)这些功能,如果对文件上传有...
带进度条的 nodeJs 文件上传 带进度条的 nodeJs 中的简单文件上传 使用的技术: NodeJs, expressJs, bootstrap, jquery npm install node app.js 如需演示,请单击此链接
使用docker-compose的nodejs todo应用程序从docker-compose up -d开始您可以通过访问127.0.0.1:3000/todos访问该应用程序。在mysql中创建表use sampleDb;CREATE TABLE `todos` ( `id` int(11) NOT NULL AUTO_...
nodejs文件上传和csv解析该项目部署了Express框架和fast-csv模块,以实现文件上传和cvs文件解析的功能。 测试基于供应商提供的购物清单csv文件,并且我还提供了提交纯购物表单的功能。购物形式在类别下显示税率。 在...
cd nodejs-file-stucture-express npm install or npm i npm run dev (For Development environment) OR npm run staging (For Staging environment) OR npm run prod (For Production environment) 贡献 欢
aot-nodejs-feedparser-sample 这是示例代码,以及我。 运行项目 在开始之前,请确保已安装NodeJS0.10.x。 在终端上,运行: $ cd FeedparserSample/ $ npm install 跑步: $ node app.js 用法示例 将您的浏览器...
【aws-nodejs-sample】是一个基于Amazon Web Services (AWS) 和Node.js开发的示例项目。这个项目旨在帮助开发者了解如何在AWS环境中构建和部署Node.js应用程序。Node.js是一种流行的JavaScript运行环境,它允许...
本文介绍了Node Js 使用KOA处理form-data格式传输过来的文件,分享给大家。...form表单【注意:enctype=”multipart/form-data”】上传文件时,首先会将文件上传到你本机的temp目录,然后执行move_upload_file
在Node.js环境中,SQLite3是一个常用的轻量级数据库,它不需要单独的服务进程,可以直接在内存中或磁盘上创建数据库文件。本篇文章将深入探讨如何使用`node-sqlite3`库来操作SQLite3数据库,并对其进行封装,以便于...
git clone https://github.com/rahil471/File-upload-Angular2-Nodejs.git file-upload 导航到节点应用程序cd file-upload/node-app 安装依赖项npm install 全局npm install gulp -g 启动节点服务器gulp 打开一...
通过构建Docker映像在本地进行测试(检查Docker桌面应用程序是否正在运行)。 码头工人建设。 -t节点EC2-测试 通过运行该应用程序进行本地测试。 暴露端口3000 ... 泊坞窗运行-p 3000:3000 node-ec2-test
使用nodejs+express4实现了文件上传、下载、删除、列表展示等功能,比网上那些只有上传下载的更加全面,关键是你可以拿来就用同时又能学到nodejs和express的知识,何乐不为?
Node.js文件服务器 ... 安装NodeJS,NPM / Yarn(如果尚未安装) 使用npm install或yarn install软件包 使用node index.js运行代码 要上传文件: 造访/上传 上载! 要查看上传的文件: 访问/filename.ext
总的来说,“nodejs中jquryfileupload异步上传的demo”展示了如何在Node.js环境中利用jQuery File Upload和multer实现文件的异步上传。通过理解这一过程,开发者能够构建出高性能、用户友好的文件上传功能。
使用 nodejs 的文件服务器 安装 cd /directory/to/be/shared/via/http curl -O https://raw.githubusercontent.com/nestoru/file-server.js/master/file-server.js curl -O ...
Nodejs条带样本入门先决条件 > = v0.12.0 ( npm install --global bower ) ,然后gem install sass ( npm install --global grunt-cli ) 与mongod运行一个守护进程发展在./client/app/purchase/puchase....
nodejs初级教程
aws-nodejs-sample, 示例项目以演示 node.js的AWS SDK用法 用于 node.js 示例项目的 AWS一个简单的node.js 应用程序,演示了用于 Node.js.的AWS SDK要求这里应用程序的唯一要求是 node 包管理器。 所有其他依赖项( ...
ng-file-upload 用于将多部分文件上传到 S3测试Jasmine 测试通过前端的业力运行。 Mocha 测试通过后端的 grunt 运行。入门安装 Node.js 使用安装程序 或者使用 macports sudo port install nodejs 或者使用 ...