I have used the guide below to deploy to the server:
https://steveholgado.com/nginx-for-nextjs/
API Routers working well in postman and start local with command "next start" or "next start-local" .
Folder Structure:
Pages
---api/
------/redirect
---------------/index.js
---home
this is my next.config.js code:
require("dotenv").config();
const webpack = require("webpack");
const withPlugins = require("next-compose-plugins");
const withCss = require("@zeit/next-css");
let pathBaseDomain = process.env.API_URL_OTP;
if (process.env.NODE_ENV === "production") {
if (typeof window !== "undefined") {
pathBaseDomain = window._env_.API_URL_OTP;
} else {
pathBaseDomain = process.env.API_URL_OTP;
}
}
const nextConfig = {
// target: "serverless",
webpack(config) {
config.module.rules.push({
test: /\.(png|svg|eot|otf|ttf|woff|woff2|gif)$/,
use: {
loader: "url-loader",
options: {
limit: 8192,
publicPath: "/_next/static/",
outputPath: "static/",
name: "[name].[ext]"
}
}
});
config.plugins.push(new webpack.EnvironmentPlugin(process.env));
return config;
},
assetPrefix: process.env.NODE_ENV === 'production' ? pathBaseDomain : '',
publicRuntimeConfig: {
basePath:process.env.NODE_ENV === 'production' ? pathBaseDomain : '',
},
};
module.exports = withPlugins([[withCss]], nextConfig);