/* * @Author: Johnhong9527 * @Date: 2019-06-04 16:44:30 * @Last Modified by: Johnhong9527 * @Last Modified time: 2019-06-04 17:03:07 */ const express = require('express'); const app = express(); const fs = require('fs'); var formidable = require('formidable'); //allow custom header and CORS app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header( 'Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild' ); res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); if (req.method == 'OPTIONS') { res.send(200); /让options请求快速返回/; } else { next(); } }); app.post('/upload.do', (req, res) => { var form = new formidable.IncomingForm(); console.log('about to parse'); form.parse(req, function(error, fields, files) { console.log('parsing done'); console.log(files); fs.writeFileSync('public/test.png', fs.readFileSync(files.upload.path)); res.redirect('/public/upload.html'); res.send({ uid: Date.parse(new Date()) / 1000, url: '' }); }); /*作者:深蓝一人 链接:https://juejin.im/post/5a1a80a5f265da43333e1b88 来源:掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/ }); app.get('/', (req, res) => { res.send('hello'); }); app.listen(3333, function() { console.log('to http://localhost:3333'); });