dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.74k stars 167 forks source link

Next + serverless-http #77

Closed romainquellec closed 5 years ago

romainquellec commented 5 years ago

I'm trying to make nextJS work with serverless-http. It's all right except for assets (css, js, ...) Can someone take a look ? I have no solution.

https://github.com/CuistotduCoin/front

NextJs issue is there : https://github.com/zeit/next.js/issues/6309

romainquellec commented 5 years ago

Current lambda is :

const compression = require("compression");
const express = require("express");
const { parse } = require("url");
const serverless = require("serverless-http");
const pathMatch = require("path-match");
const isProduction = process.env.NODE_ENV === "production";

// setup Express and hook up Next.js handler
const app = express();
app.use(compression());

const route = pathMatch();
const matches = [];

// host the static files
app.use("/_next/static", express.static("../static"));
app.use("/static", express.static("../static"));

app.get('/', require('./serverless/pages/index').render)
app.get('*', (req, res) => {

  const parsedUrl = parse(req.url, true);
  const { pathname, query } = parsedUrl;
  let hasMatch = false;

  for (const match of matches) {
    const params = match.route(pathname);
    if (params) {
      try {
        require(`./serverless/pages${pathname}`).render(req, res, match.page, Object.assign(params, query))
      } catch (err) {
        require('./serverless/pages/_error').render(req, res, match.page, Object.assign(params, query))
      }
      hasMatch = true;
      break;
    }
  }
  if (!hasMatch) {
    try {
      require(`./serverless/pages${pathname}`).render(req, res, parsedUrl)
    } catch (err) {
      require('./serverless/pages/_error').render(req, res, parsedUrl)
    }
  }
})

// 404 handler
app.get("*", require('./serverless/pages/_error').render);

// export the wrapped handler for the Lambda runtime
exports.handler = serverless(app);

(It might change for testing purpose)

romainquellec commented 5 years ago

Error is on my side.