简体   繁体   English

为Next.js的服务器文件设置自定义目录

[英]Setting custom directory for server files for Next.js

Rather than in the root directory, I want to keep all my backend related files inside a folder named 'server'. 我想将所有与后端相关的文件都保存在名为“ server”的文件夹中,而不是在根目录中。 The problem is now the frontend won't load properly as it can't find the 'pages' directory. 现在的问题是前端无法正确加载,因为它找不到'pages'目录。 I remember there was a way to set the directory somehow when initializing the app but I don't remember the specifics. 我记得在初始化应用程序时可以通过某种方式设置目录,但是我不记得具体细节。 Can someone please help me with this? 有人可以帮我吗?

server/index.js: 服务器/ index.js:

const express = require('express')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ 
  dev, 
  // some config property that I don't remember
})
app.prepare().then(() => {
    const server = express()
    server.listen(3000, () => console.log('> Ready on http://localhost:3000'))
})

You can read from the documentation : 您可以阅读文档

The next API is as follows:

next(opts: object)
Supported options:

dev (bool) whether to launch Next.js in dev mode - default false
dir (string) where the Next project is located - default '.'
quiet (bool) Hide error messages containing server information - default false
conf (object) the same object you would use in next.config.js - default {}

Then, change your start script to NODE_ENV=production node server.js.

It is dir option. 它是dir选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM