使用Express.js,如何在任何请求下发送文件?

时间:2018-12-29 12:41:05

标签: node.js express

我正尝试在任何获取请求上发送UIBezierPath.reversing()

代码:

let cgPath: CGPath = ...
let size = cgPath.boundingBox.size

let inverted = UIBezierPath(rect: CGRect(origin: .zero, size: size))
inverted.append(UIBezierPath(cgPath: cgPath).reversing())

//draw result
let image = UIGraphicsImageRenderer(size: size).image { context in
    context.cgContext.setFillColor(UIColor.red.cgColor)
    context.cgContext.addPath(inverted.cgPath)
    context.cgContext.fillPath()
}

enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

您可以在此处使用的一种不错的技术是将html页存储在ram中,这样您的系统就不必在每次发出请求时都读取文件。我建议您使用fs库。请尝试以下操作。

import express from 'express';
const fs = require('fs')

const app = express();

var indexPage = fs.readFileSync(__dirname + '/PATH_TO/index.html', 'utf8')

app.get('*', function (req, res) {
   return res.send(indexPage)
})

app.listen(8000, () => console.log('Server started on port 8000'));

使用当前的解决方案,您尝试为每个请求读取文件。通过执行上述操作,您将节省大量CPU功率并提供更快的结果。

答案 1 :(得分:1)

更改您的

app.use(express.static(path.join(__dirname, '..', 'dist')));

app.get('*', function (req, res) {
  res.sendFile(path.join(__dirname, '..', 'dist', 'index.html'));
  res.end();
});

app.use(express.static(path.join(__dirname, '..', 'dist')));

app.get('*', function (req, res) {
    res.sendFile('index.html', {root: path.join(__dirname, '..', 'dist')});
    res.end();
});