im试图在nextjs应用中设置自定义快递服务器以托管到Firebase。
./ server.js
const express = require('express');
const next = require('next');
const path = require('path');
const bodyParser = require('body-parser');
const product = require('./server/routes/product');
const productHover = require('./server/routes/product-hover');
const checkout = require('./server/routes/checkout');
const { https } = require('firebase-functions')
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dir: '.', dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
// Static files
// https://github.com/zeit/next.js/tree/4.2.3#user-content-static-file-serving-eg-images
server.use('/images', express.static(path.join(__dirname, 'images'), {
maxAge: dev ? '0' : '365d'
}));
server.use(bodyParser.json());
/**
* @route POST api/upload
* @route POST api/upload-hover S
* @desc Upload post image
* @access public
*/
server.use('/api/upload', product);
server.use('/api/upload-hover', productHover);
server.use('/api/checkout', checkout);
server.get('*', (req, res) => {
return handle(req, res)
});
const PORT = process.env.PORT || 3001;
server.listen(PORT, (err) => {
if (err) throw err
console.log(`> Read on http://localhost:${PORT}`)
});
})
在本地可以正常工作,因此请遵循有关以下内容的教程:
https://itnext.io/deploying-next-js-app-to-firebase-functions-eb473791d79e
我确实尝试过:
const express = require('express');
const next = require('next');
const path = require('path');
const bodyParser = require('body-parser');
const product = require('./server/routes/product');
const productHover = require('./server/routes/product-hover');
const checkout = require('./server/routes/checkout');
const { https } = require('firebase-functions')
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dir: '.', dev });
const handle = app.getRequestHandler();
exports.server = https.onRequest(async (req, res) => {
return app.prepare().then(() => {
const server = express();
// Static files
// https://github.com/zeit/next.js/tree/4.2.3#user-content-static-file-serving-eg-images
server.use('/images', express.static(path.join(__dirname, 'images'), {
maxAge: dev ? '0' : '365d'
}));
server.use(bodyParser.json());
/**
* @route POST api/upload
* @route POST api/upload-hover S
* @desc Upload post image
* @access public
*/
server.use('/api/upload', product);
server.use('/api/upload-hover', productHover);
server.use('/api/checkout', checkout);
server.get('*', (req, res) => {
return handle(req, res)
});
const PORT = process.env.PORT || 3001;
server.listen(PORT, (err) => {
if (err) throw err
console.log(`> Read on http://localhost:${PORT}`)
});
})
})
但是响应是服务器无法处理请求,请有人可以帮助我!