1、图片文件
需要依赖
const express = require('express')//express框架 const multer = require('multer');//进行文件上传接口需要使用 multer中间件 const fileFun = require('./utils/flieFun/fileReder')//引用读取文件工具方法 const fs = require('fs')//读写文件中间件 const path = require('path')//使用相对路径需要 const log4js = require('./utils/logger')//node.js中日志管理插件 fileFun 读取文件方法封装const fs = require('fs'); const newpath = require('path') //读取文件 function readFile(path) { return new Promise((resolve, reject) => { fs.readFile(newpath.join(__dirname,path), function (error, data) { if (error) { reject(error) } else { resolve(data) } }) }) }
function readJsonFile(path) { return new Promise((resolve, reject) => { fs.readFile(newpath.join(__dirname, path), 'utf-8', function (error, data) { if (error) { reject(error) } else { resolve(data) } }) }) } module.exports = { readFile, readJsonFile }
log4js日志系统工具类抒写
const log4js = require("log4js")
// logger configure log4js.configure({ appenders: { out: { type: 'console' }, task: { type: 'dateFile', filename: 'logs/task', "pattern": "_yyyy-MM-dd.log", alwaysIncludePattern: true }, result: { type: 'dateFile', filename: 'logs/result', "pattern": "_yyyy-MM-dd.log", alwaysIncludePattern: true }, error: { type: 'dateFile', filename: 'logs/error', "pattern": "_yyyy-MM-dd.log", alwaysIncludePattern: true }, default: { type: 'dateFile', filename: 'logs/default', "pattern": "_yyyy-MM-dd.log", alwaysIncludePattern: true }, rate: { type: 'dateFile', filename: 'logs/rate', "pattern": "_yyyy-MM-dd.log", alwaysIncludePattern: true } }, categories: { default: { appenders: ['out', 'default'], level: 'info' }, task: { appenders: ['task'], level: 'info' }, result: { appenders: ['result'], level: 'info' }, error: { appenders: ['error'], level: 'error' }, rate: { appenders: ['rate'], level: 'info' } }, replaceConsole: true });
module.exports = log4js.getLogger('logger');
上传图片文件
const router = express.Router()
//生成的图片放入uploads文件夹下
var upload = multer({ dest: 'uploads/' })
// 图片上传
router.post('/upload', upload.single('test'), function (req, res, next) {
log4js.info('/upload....入参'+req.file)
//读取文件路径(uploads/文件夹下面的新建的图片地址)
fs.readFile(req.file.path, (err, data) => {
//如果读取失败
if (err) { log4js.error('/upload....错误' + err); return res.send('上传失败') }
//如果读取成功
//声明图片名字为时间戳和随机数拼接成的,尽量确保唯一性
let time = Date.now() + parseInt(Math.random() * 999) + parseInt(Math.random() * 2222);
//拓展名
let extname = req.file.mimetype.split('/')[1]
//拼接成图片名
let keepname = time + '.' + extname
//三个参数
//1.图片的绝对路径
//2.写入的内容
//3.回调函数
fs.writeFile(path.join(__dirname, '/public/images/' + keepname), data, (err) => {
if (err) { log4js.error('/upload....写入文件错误' + err); return res.send('写入失败') }
log4js.info('/upload....成功' + { code: 0, msg: '上传ok', data: '/public/images/' + keepname });
res.send({ issuccessful: true, msg: '上传ok', data: '/public/images/' + keepname })
});
});
});
读取图片文件(可生成线上图片)
//获取图片
router.get(/[/]public[/]images[/]\w+[.]((jpg)|(png)|(jpeg))/, function (req, res, next) {
log4js.info(req.url+'....入参' + req.url)
if (req.url.indexOf(".jpg") != -1 || req.url.indexOf(".jpeg") != -1) {
res.writeHead(200, { 'Content-Type': 'image/jpeg' });
fileFun.readFile("../.."+req.url).then((reslut, error) => {
if (error) {
log4js.error(req.url + '....错误:' + err);
res.send(error)
} else {
log4js.info(req.url + '....接口调用成功');
res.send(reslut)
}
})
} else if (req.url.indexOf(".png")!=-1) {
res.writeHead(200, { 'Content-Type': 'image/png' });
fileFun.readFile("../.."+req.url).then((reslut, error) => {
if (error) {
log4js.error(req.url + '....错误:' + err);
res.send(error)
} else {
log4js.info(req.url + '....接口调用成功');
res.send(reslut)
}
})
} else {
log4js.error(req.url + '....错误:' + { code: 400, msg: "失败" });
res.send({ issuccessful:false, msg: "失败"})
}
})