我正在使用Next js和Express服务器。
应用运行时,出现下一个错误:
此网站无法提供安全的连接
本地主机发送了无效的响应。
ERR_SSL_PROTOCOL_ERROR
如何避免这个问题?
package.json:
"scripts": {
"dev": "node ./src/index.js",
"build": "next build ./client",
"start": "NODE_ENV=production node ./src/index.js",
"test": "nyc --reporter=html --reporter=text mocha --timeout 15000 --reporter mochawesome --exit",
"lint": "eslint '**/*.{js,jsx}' --quiet",
"format": "prettier --write '**/*.{js,jsx,css,scss}'"
},
server.js
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.use(bodyParser.json()); // support json encoded bodies
server.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
const forceHttps = process.env.FORCE_HTTPS !== 'false';
if (forceHttps) {
// Forcing redirect to HTTPS
server.use((req, res, cb) => {
if (!/https/.test(req.protocol) && req.headers['x-forwarded-proto'] !== 'https') {
res.redirect(`https://${req.headers.host}${req.url}`);
} else {
cb();
}
});
}
server.get('*', (req, res) => handle(req, res));
server.listen(port, (err) => {
if (err) {
logger.error('something bad happened', err);
} else {
logger.info(`> Ready on http://localhost:${port}`);
}
});
});
```