我已经实现了一个快速服务器,它使用express.static为从静态文档库站点创建的构建文件夹提供服务,以便应用基本身份验证访问站点。这在本地运行得很好,但是我在部署到Vercel时遇到了麻烦。
目前,我的配置允许在vercel上部署的版本呈现基本的auth登录页面,但是在成功登录时,我被定向到一个页面,状态是:“无法获取/”
我相信这可能是我的vercel.json配置或vercel模板设置的一个问题。
我的代码如下:
index.mjs
import express from 'express';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(express.json());
const authorize = ((req, res, next) => {
const auth = {login: process.env.USERNAME, password: process.env.PASSWORD}
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':')
if (login && password && login === auth.login && password === auth.password) {
return next()
}
res.set('WWW-Authenticate', 'Basic realm="401"')
res.status(401).send('Authentication required.')
});
app.use('/', authorize);
app.use('/', express.static('build'));
app.listen(3000);
console.log(` Server ready at http://localhost:3000`);vercel.json
{
"version": 2,
"builds": [{
"src": "./index.mjs",
"use": "@vercel/node"
}],
"routes": [{"handle": "filesystem"},
{
"src": "/.*",
"dest": "/"
}
]
}package.json -启动脚本
"start": "node --experimental-modules index.mjs",我的vercel模板被设置为other,start脚本设置为npm。
任何想法都将不胜感激!
发布于 2022-07-15 20:09:13
我也有类似的问题:提供一些静态内容并使用/api路由。所有这些都在本地开发中工作得很好,但是在vercel中抛出的“不能得到/”。
我的解决方案。
我的最后一个vercel.json
{
"version": 2,
"builds": [
{
"src": "server.js",
"use": "@vercel/node"
},
{
"src": "public/**",
"use": "@vercel/static"
}
],
"routes":[
{
"src": "/api/(.*)",
"dest": "server.js"
},
{
"src": "/",
"dest": "public/index.html"
},
{
"src": "/(.+)",
"dest": "public/$1"
}
]
}备注
所有静态内容都在etc)
Generated build outputs:
12:28:55.974 - Static files: 12
12:28:55.974 - Serverless Functions: 1
12:28:55.975 - Edge Functions: 0
...
12:28:58.142 Done with "server.js"也许从现在开始,你可以为你的案子找到一个解决方案。
https://stackoverflow.com/questions/72133185
复制相似问题