Recently, I came up with a problem that I need to upload images to a Node server and display the image instantly. I write this post to keep a record of what I have done.
Generally, firstly I wrap the uploading image as a FormData.
|
|
Then I use jquery .ajax()
method to send POST
request to the server.
|
|
At server, formidable middleware is used to handle the uploading file.
|
|
Note: A working demo is available in github.
The demo could be used like this:
|
|
File Structure
- server.js is Node server, which contains routes and main logic of back end
- .env stores environmenal variable while developing. A node module called
dotenv
can be used to load variables in.env
file to global variableprocess
- package.json contains information of this project and dependencies
- /public contains javascript, css, fonts and images, which will be used in front end
- /views contains HTML templates
- /uploads stores uploaded images
Written with StackEdit.