You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
V3D-nodejs/app.js

62 lines
1.6 KiB

6 years ago
var express = require('express');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
6 years ago
var bodyParser = require('body-parser');
6 years ago
var fs = require('fs');
var gm = require('gm');
6 years ago
var app = express();
6 years ago
app.use(bodyParser.json({ limit: '50mb' }));
6 years ago
app.use(express.static('static'));
6 years ago
//临时存储数据
var data = { name: '默认方案', components: [] };
6 years ago
//主页加载
app.get('/', function (req, res) {
res.sendFile(__dirname + "/views/createScheme.html");
});
//接受文件上传,并且返回文件名
app.post('/upload', upload.single('file'), function (req, res) {
//console.info(req.file)
res.send(req.file);
});
6 years ago
//返回文件
6 years ago
app.get('/files/:filename', function (req, res) {
6 years ago
//TODO 根据session判断是否有权限获取文件
6 years ago
var filename = req.params['filename'];
res.sendFile(__dirname + "/uploads/" + filename);
6 years ago
});
6 years ago
//返回缩略图
app.get('/files/thumbnail/:filename', function (req, res) {
var filename = req.params['filename'];
fs.exists(__dirname + "/uploads/thumbnail/" + filename, function (exist) {
if (exist) {
res.sendFile(__dirname + "/uploads/thumbnail/" + filename);
} else {
gm('./uploads/' + filename).resize(40, 40).write("./uploads/thumbnail/" + filename, function (err) {
if (!err)
console.info(err);
res.sendFile(__dirname + "/uploads/thumbnail/" + filename);
})
}
});
});
6 years ago
//保存方案
app.post('/saveScheme', function (req, res) {
data = req.body;
});
//获取方案
app.post('/getScheme', function (req, res) {
res.send(data);
});
6 years ago
app.listen(80);