可以在next.js中创建文件上传api吗?(使用 /api 目录)还是我必须在线使用外部第三方服务?网上找不到任何例子。谢谢
我在 MongoDB Atlas 上看到了很多关于此警报的帖子和文章(“配置限制的连接百分比已超过 80”),但不知道如何在我的 Next.js 应用程序中解决它。
我在处理程序函数之外创建数据库连接。我使用了一个中间件withDatabase.js
:
const client = new MongoClient(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const addDbToRequest = (handler, req, res) => {
req.db = req.connection.db("MYDBNAME");
return handler(req, res);
};
const withDatabase = handler => async (req, res) => {
if (!client.isConnected()) {
await client.connect();
}
req.connection = client;
return addDbToRequest(handler, req, res);
};
export default withDatabase;
Run Code Online (Sandbox Code Playgroud)
该中间件包装了 API 端点处理程序。
现在,如果我在每个 API 处理程序完成时关闭连接,如下所示:
const { connection } = req;
if (connection) {
connection.close();
} …
Run Code Online (Sandbox Code Playgroud) 假设我有一个名为 utils.js 的文件,其中包含两个函数 s 和 c。s 是一个服务器端函数(在 /api 端点处理程序上调用),并使用 mongodb 包。c 是客户端函数(将被捆绑并发送到浏览器)。
使用下一个版本编译应用程序时,会导致任何问题吗?webpack 是否知道仅捆绑文件/模块的一部分?(将服务器端函数和导入视为“死代码”,因为它们仅从服务器端代码调用)
谢谢
尝试使用target: 'serverless'
in构建我的应用程序next.config.js
(为了在 AWS Lambda 上部署)。运行时,npm run build
我得到以下输出:
Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.
See here for more info: https://err.sh/next.js/built-in-css-disabled
info - Using external babel configuration from C:\Users\User\Desktop\mysite\.babelrc
info - Creating an optimized production build
warn - Compiled with warnings
./node_modules/require_optional/index.js
Critical dependency: the request of a dependency is an expression
./node_modules/node-pre-gyp/lib/pre-binding.js
Critical dependency: the request of a dependency is an expression
./node_modules/node-pre-gyp/lib/util/versioning.js
Critical dependency: the request …
Run Code Online (Sandbox Code Playgroud) 下面这句话大家都很熟悉:You should not use getInitialProps, but getServerSideProps instead
. 我已经使用构建了我的应用程序getInitialProps
,现在我面临以下问题:“我是否应该将 getInitialProps 替换为 getServerSideProps?”,如果是这样,我应该如何以正确的方式进行操作而不损坏我的应用程序?
今天,我getInitialProps
在几个重要的地方使用:
_app.js
中react-redux
:class MyApp extends App {
static async getInitialProps({ Component, ctx }) {
return {
pageProps: {
...(Component.getInitialProps
? await Component.getInitialProps(ctx)
: {})
}
};
}
render() {
const { Component, pageProps, store } = this.props;
return (
<>
<Provider store={store}>
<Component {...pageProps} />
</Provider>
</>
);
}
}
export default withRedux(initStore)(withReduxSaga(MyApp));
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我将请求发送到getInitialProps
api 端点(在 /api 文件夹中)。在本地主机中运行我的应用程序时,一切正常,因为它都在同一域(本地主机)下。
将我的应用程序成功部署到 aws lambda (使用serverless-next.js
)后,这些请求不再有效。
原因是getInitialProps
在服务器中运行时的主机头是myAppBucket.s3.us-east-1.amazonaws.com
,但应用程序域是https://d25q7fh11ll2cg.cloudfront.net
。请求myAppBucket.s3.us-east-1.amazonaws.com/api/users
不起作用但起作用https://d25q7fh11ll2cg.cloudfront.net/api/users
。https://d25q7fh11ll2cg.cloudfront.net/api/users
但我运行时没有域数据(即) getInitialProps
。
我不想将域作为环境变量。还有另一种解决方案吗?此外,直接调用处理程序函数getInitialProps
也是有问题的,因为处理程序仅使用服务器端包。谢谢