English 中文(简体)
我如何在Vercel上通过节点管理我的建筑而不在我的 URL 中使用 / api?
原标题:How can I run my build through node on Vercel without using /api in my URL?

I am trying to deploy my react application on vercel .But i want to run my build using node server. Because I have to use prerender.io in my node app. Such that preview can come with dynamic value when someone share the link.

因为我要在我的根文件夹中添加服务器. js, 这就是代码 :

const express = require( express );
const prerender = require( prerender-node );

const app = express();

// Serve the static React build files
app.use(express.static( build ));

// Use prerender.io for prerendering
app.use(prerender.set( prerenderToken ,  MY TOKEN ));

// Serve the React app for all routes
app.get( * , (req, res) => {
  res.sendFile(path.resolve(__dirname,  build ,  index.html ));
});

// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

这是我的动脉,json:

{
    "rewrites": [
      { "source": "/(.*)", "destination": "/server.js" }
    ]
  }
  

但我的建筑不是通过节点读取,

But when i added api folder in my app and added same server.js code there , afterwards it started running through node app. And i dont want to use /api in my url for it to work. So what can i walk around such that my build runs through node app .

这些是我包裹里的剧本,杰森

 "scripts": {
    "start": "node server.js",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },```



问题回答

您的vercel 配置文件格式错误

Rewrites change the path from source with the destination, but keep in mind that the vercel internal routes are different.

In this example, your api is under "/api/server" then you need to point to that path

在你的动脉中,json需要改变到这个

{
  "version": 2,
  "rewrites": [{ "source": "/(.*)", "destination": "/api/server" }]
}




相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签