小编use*_*396的帖子

在 next.js 中创建上传文件 api

可以在next.js中创建文件上传api吗?(使用 /api 目录)还是我必须在线使用外部第三方服务?网上找不到任何例子。谢谢

next.js

14
推荐指数
1
解决办法
1万
查看次数

next.js 和 mongodb atlas - 收到“配置限制的连接百分比已超过 80”警报

我在 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)

mongodb next.js mongodb-atlas

7
推荐指数
1
解决办法
2106
查看次数

Next.js - 在包含服务器端和客户端功能的文件中导入服务器端包?

假设我有一个名为 utils.js 的文件,其中包含两个函数 s 和 c。s 是一个服务器端函数(在 /api 端点处理程序上调用),并使用 mongodb 包。c 是客户端函数(将被捆绑并发送到浏览器)。

使用下一个版本编译应用程序时,会导致任何问题吗?webpack 是否知道仅捆绑文件/模块的一部分?(将服务器端函数和导入视为“死代码”,因为它们仅从服务器端代码调用)

谢谢

next.js

6
推荐指数
1
解决办法
4095
查看次数

Next.js - `npm run build` 失败

尝试使用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)

javascript next.js

6
推荐指数
1
解决办法
2627
查看次数

如何安全地用 getServerSideProps / getStaticProps 替换 getInitialProps

下面这句话大家都很熟悉:You should not use getInitialProps, but getServerSideProps instead. 我已经使用构建了我的应用程序getInitialProps,现在我面临以下问题:“我是否应该将 getInitialProps 替换为 getServerSideProps?”,如果是这样,我应该如何以正确的方式进行操作而不损坏我的应用程序?

今天,我getInitialProps在几个重要的地方使用:

  1. 在我的习惯_app.jsreact-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)
  1. 在页面的一些 HOC 中:检查用户是否通过身份验证(否则重定向),如果用户通过身份验证,则初始化 redux 存储,获取用户信息。 …

next.js getserversideprops

5
推荐指数
0
解决办法
594
查看次数

从 getInitialProps 向 /api 文件夹发出请求

在我的应用程序中,我将请求发送到getInitialPropsapi 端点(在 /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/usershttps://d25q7fh11ll2cg.cloudfront.net/api/users但我运行时没有域数据(即) getInitialProps

我不想将域作为环境变量。还有另一种解决方案吗?此外,直接调用处理程序函数getInitialProps也是有问题的,因为处理程序仅使用服务器端包。谢谢

aws-lambda next.js serverless

0
推荐指数
1
解决办法
1964
查看次数