首页 > 解决方案 > 在此服务器上未找到请求的 URL /about-us

问题描述

执行导出后,在主机上,当我手动输入链接或刷新页面时,没有显示消息。我有 server.js 和下一个模块。我还需要做其他事情吗?

服务器.js

const express = require('express');
const next = require('next');
const { parse } = require('url');

const DEV = process.env.ENVIRONMENT !== 'production';
const PORT = 4567;

const app = next({dir: '.', dev: DEV});
const handle = app.getRequestHandler();

const getRoutes = require('./routes');

const routes = getRoutes();
app.prepare().then(() => {
  const server = express();
  server.get('*', (req, res) => {
    const parsedUrl = parse(req.url, true);
    const { pathname, query } = parsedUrl;
    const route = routes[pathname];
    if (route) {
      return app.render(req, res, route.page, route.query);
    }
    return handle(req, res);
  });

  server.listen(PORT, (err) => {
    if (err) throw err;
    console.log(`> READY FOR LIFOTFF http://localhost:${PORT}`);
  });
});

路由器.js

module.exports = () => {
    return {
      '/': { page: '/' },
      '/404': { page: '/404' },
      '/about-us': { page: '/about-us' },
      
    }
  }

next.config.js

const getRoutes = require('./routes');
module.exports = {
    useFileSystemPublicRoutes: true,
    exportPathMap: getRoutes

  }

标签: javascriptnode.jsreactjsnext.js

解决方案


我添加trailingSlash: true到 next.config.js,它正在工作。

我的新next.config

const getRoutes = require('./routes');
module.exports = {
    trailingSlash: true, 
    useFileSystemPublicRoutes: true,
    exportPathMap: getRoutes,
    serverRuntimeConfig: {
      // Will only be available on the server side
      mySecret: 'secret',
      secondSecret: process.env.SECOND_SECRET, // Pass through env variables
    },
    publicRuntimeConfig: {
      // Will be available on both server and client
      staticFolder: '/static',
    },

  }

推荐阅读