Nodejs is a young programming framework, full of vitality and unlimited passion, and has always been updated rapidly. Express, the official web development library based on Nodejs, is also developing simultaneously, with a major version upgrade every year and even major surgery on the bottom layer of the framework. In Express4, the middleware library connect was replaced by multiple more fine-grained libraries. The obvious benefit is that these middleware can be updated and released more freely and will not be affected by the Express release cycle; but the problem is also very difficult. It is not compatible with previous versions, and upgrading means modifying the code.
After a period of research and exploration, I found that the ways to achieve uploading are: 1. express middleware multer module (this is the most efficient, natively supported in express3.x, and became an independent module in express4.x), 2. connect-multiparty module (but it is not officially recommended now), 3. Use the multiparty module to implement (this method is more common), 4. Use the formidable plug-in to implement (the plug-in is simple and easy to understand);
The simplest way is to upload through the "connect-multiparty" middleware.
Install by npm install connect-multiparty in the project.
Usage:
var multipart = require('connect-multiparty'); var multipartMiddleware = multipart(); app.post('/upload', multipartMiddleware, function(req, resp) { console.log(req.body, req.files); // don't forget to delete all req.files when done });
After uploading, the uploaded file will generate a temporary file in the temporary directory. You can print out req.files to view the specific file path.
Just move and rename the temporary file to the actual directory in the commented place to complete the upload function.
Simple.
Official address: https://www.npmjs.com/package/connect-multiparty
However, the official does not recommend using this middleware. It is recommended to use "multiparty" directly because error handling is more troublesome.
Let’s use “multiparty” to implement a version.
1. Use express (version 4.11.x) to create a project, using the default jade as the template engine.
2. In the project directory, install the necessary components through npm install multiparty.
3. Modify views/index.jade and make a simple form for file upload as follows.
extends layout block content form(method='post', action='/file/uploading', enctype='multipart/form-data') input(name='inputFile', type='file', multiple='mutiple') input(name='btnUp', type='submit',value='上传')
4. Modify routes/index.js to implement the background code for uploading pages and uploading responses.
var express = require('express'); var router = express.Router(); var multiparty = require('multiparty'); var util = require('util'); var fs = require('fs'); /* 上传页面 */ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); /* 上传*/ router.post('/file/uploading', function(req, res, next){ //生成multiparty对象,并配置上传目标路径 var form = new multiparty.Form({uploadDir: './public/files/'}); //上传完成后处理 form.parse(req, function(err, fields, files) { var filesTmp = JSON.stringify(files,null,); if(err){ console.log('parse error: ' + err); } else { console.log('parse files: ' + filesTmp); var inputFile = files.inputFile[]; var uploadedPath = inputFile.path; var dstPath = './public/files/' + inputFile.originalFilename; //重命名为真实文件名 fs.rename(uploadedPath, dstPath, function(err) { if(err){ console.log('rename error: ' + err); } else { console.log('rename ok'); } }); } res.writeHead(, {'content-type': 'text/plain;charset=utf-'}); res.write('received upload:\n\n'); res.end(util.inspect({fields: fields, files: filesTmp})); }); }); module.exports = router;
Done. This completes the introduction to the file upload function based on nodejs express (4.x). I hope it will be helpful for everyone to learn nodejs express related knowledge.

怎么处理文件上传?下面本篇文章给大家介绍一下node项目中如何使用express来处理文件的上传,希望对大家有所帮助!

Express和Laravel是两个非常流行的Web框架,分别代表了JavaScript和PHP两大开发语言的优秀框架。本文将针对这两个框架进行对比分析,帮助开发者选择更适合自己项目需求的框架。一、框架简介Express是一个基于Node.js平台的Web应用程序框架,它提供了一系列强大的功能和工具,使开发者可以快速搭建高性能的Web应用程序。Express

深入比较Express和Laravel:如何选择最佳框架?在选择一个适合自己项目的后端框架时,Express和Laravel无疑是两个备受开发者欢迎的选择。Express是基于Node.js的轻量级框架,而Laravel则是基于PHP的流行框架。本文将深入比较这两个框架的优缺点,并提供具体的代码示例,以帮助开发者选择最适合自己需求的框架。性能和扩展性Expr

Express与Laravel:对比优劣势,你会选择哪个?在Web开发领域,Express和Laravel是两个备受关注的框架。Express是一个基于Node.js的灵活且轻量级的Web应用框架,而Laravel是一个基于PHP的优雅且功能丰富的Web开发框架。本文将从功能、易用性、扩展性以及社区支持等方面对比Express和Laravel的优劣势,并结合

node+express怎么操作cookie?下面本篇文章就来给大家介绍一下用node操作cookie的方法,希望对大家有所帮助!

在选择后端框架时,Express和Laravel都是非常流行的选择。Express是基于Node.js的Web应用程序开发框架,而Laravel是基于PHP的Web应用程序开发框架。两者各有优势,选择最适合自己的框架需要考虑多方面因素。Express框架的优势在于它的灵活性和简单的学习曲线。Express的核心思想是“足够小,足够灵活”,它提供了大量的中间件

laravel好,有以下几点原因:1、Laravel使用PHP语言,具有优雅和直观的语法,并提供大量的功能和工具;2、Laravel有各种各样的扩展包、插件和解决方案;3、Laravel有非常完善和易懂的官方文档;4、Laravel对数据安全和用户认证提供了很好的支持;5、Laravel能够有效地分离业务逻辑和显示层;6、Laravel拥有庞大而活跃的社区,可以获得最新的技术。

如何使用Node.js搭建一个简单的博客系统Node.js是一个基于ChromeV8引擎的JavaScript运行时环境,可以让JavaScript的运行速度更加高效。在Node.js的帮助下,我们可以使用JavaScript构建强大的服务器端应用程序,包括博客系统。本文将向您介绍如何使用Node.js搭建一个简单的博客系统,并为您提供具体的代码示例。请按


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version
