// @see https://github.com/zeit/next.js/tree/master/examples/custom-server // @see https://gist.github.com/edolyne/10bf9cfdd1e75557c3c4c63a2c1fc0b5 const express = require('express'); const next = require('next'); const axios = require('axios'); const fs = require("fs"); const port = parseInt(process.env.PORT, 10) || 3000; const dev = process.env.NODE_ENV !== 'production'; const app = next({ dev }); const handle = app.getRequestHandler(); const { DESTINATION, createSitemap } = require("./sitemap"); app.prepare() .then(() => { const server = express(); // ... some config based on your app // This `server.get()` lets you generate a sitemap on the fly and retrive it from http://your.domain/sitemap.xml // It also create a file if you need to open it with your editor. server.get("/sitemap.xml", function(req, res) { res.header("Content-Type", "application/xml"); (async function sendXML() { let xmlFile = await createSitemap(); // Send it to the browser res.send(xmlFile); // Create a file on the selected destination fs.writeFileSync(DESTINATION, xmlFile); })(); }); // ... some config based on your app // This below is the default config. // You might have a different one base on your app based on your app server.get('*', (req, res) => { const pathname = req.originalUrl.substr(1); return handle(req, res) }); server.listen(port, (err) => { if (err) throw err console.log(`> Ready on http://localhost:${port}`) }); });