Next.js Express.js 和 MySql 应用部署

huangapple go评论111阅读模式
英文:

Next.js Express.js and MySql app deployment

问题

由于这是我在Next.js中完成的第一个私人项目,我想知道如何将其与后端和数据库一起托管。

所有的教程都是关于Next.js本身,我不确定在发布全栈应用程序时应该使用什么。

我需要在主文件夹中添加一些内容才能发布网站吗?
因为我需要更改Next.js,以便它不在本地主机上运行。

我考虑使用a2 hosting与cpanel。我还可以使用Heroku并注册自己的域名,但如何添加后端和数据库?

文件结构

英文:

Since this is the first private project that I have done in Next.js I am wondering how do I host it with backend and database.

All tutorials are about next.js alone, I am not sure what should I use in case to be able to publish full stack app.

Do I need something in main folder to be able to publish the web site?
Since I need to change next.js not to run on localhost.

I was thinking about using a2 hosting with cpanel. Also I can also use heroku and register my own domain, but how do I add backend and database

File structure

答案1

得分: 1

如果您想要拥有后端,NextJs提供了一个/api端点,您可以在其中初始化服务器端后端部分。但是,如果您想要拥有一个独立的后端,最好是初始化一个独立的后端API,并通过前端调用它。NextJs提供了一个api文件夹,您可以在其中定义不同的路由来处理不同的端点。

关于文件上传,提到的NPM包不适用,因为它是设计用于与ExpressJs一起使用的。您可以通过从前端发送请求作为FormData对象来实现文件上传,在后端将bodyParser自动设置为false,示例如下:

export const config: PageConfig = {
    api: {
        bodyParser: false,
    },
};

您可以使用类似于formidable的库来处理传入的请求。查看这个指南以获取更多信息。

英文:

If you would like to have a backend, NextJs comes with an /api endpoint where you can initialize your server side backend section. but If you would like to have a seperate backend It would be better to initiate a seperate backend api and call it through the frontend. NextJs comes with api folder where you can define different routes for your different endpoints.
Regarding file uploads The mentioned NPM Package Will not work Because It's Designed to work with ExpressJs. You can Implement file Upload by Sending Request from FrontEnd as a FormData object.in the backend the automatic bodyParser to false by defining an object as below

export const config: PageConfig = {
    api: {
        bodyParser: false,
    },
};

you can use a library like formidable to process the incoming Request. Follow this Guide for more information.

huangapple
  • 本文由 发表于 2023年5月29日 17:37:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定