Created
August 11, 2018 16:33
-
-
Save 8lane/d84f296347cb64c32974378a003d3a1b to your computer and use it in GitHub Desktop.
NextJS sitemap.xml & robots.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createServer } = require('http') | |
const { parse } = require('url') | |
const next = require('next') | |
const express = require('express') | |
const app = next({dev: process.env.NODE_ENV !== 'production'}) | |
const handler = app.getRequestHandler() | |
app.prepare().then(() => { | |
const server = express(); | |
const robotsOptions = { | |
root: __dirname + '/static/', | |
headers: { | |
'Content-Type': 'text/plain;charset=UTF-8', | |
} | |
}; | |
server.get('/robots.txt', (req, res) => ( | |
res.status(200).sendFile('robots.txt', robotsOptions) | |
)); | |
const sitemapOptions = { | |
root: __dirname + '/static/', | |
headers: { | |
'Content-Type': 'text/xml;charset=UTF-8', | |
} | |
}; | |
server.get('/sitemap.xml', (req, res) => ( | |
res.status(200).sendFile('sitemap.xml', sitemapOptions) | |
)); | |
server.use(express.static('/static')); | |
server.use('/robots.txt', express.static('/static/robots.txt')); | |
server.use(handler).listen(3000, err => { | |
if (err) throw err | |
console.log('> Ready on http://localhost:3000') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment