Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

2016/03/02

Mac 安裝 nvm (不用sudo 不用 homebrew)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

source ~/.nvm/nvm.sh

nvm ls-remote

nvm install v5.7

nvm use 5.7

nvm alias default 5.7

2015/03/05

A simple Nodejs (express) fIle upload example

In express, we usually use multer middleware to handle file upload.

First, use this package for your express application (here we assign upload destination as './uploads'

app.use(multer({ dest: './uploads/' }));

Now, we define a GET router for render upload form view and a POST router for upload

router.get('/upload/', function(req, res, next) {
 res.render('upload'); // render file upload form view
});

router.post('/upload/', function(req, res, next) {
 res.json(req.files); // return file information as json
});

below is upload.jade
extends layout

block content
  form(action="./", method="post", enctype="multipart/form-data")
    input(type="file", name="uploadedFile")
    input(type="submit")

Done!
You can access http://localhost:3000/upload/ to upload your files and see them in folder /uploads/ !!