require('dotenv').config() const { send } = require('micro') const files = require('serve-handler') const robotsTxt = `User-agent: * Disallow: / ` const staticPaths = [ '/css', '/images', '/archive' ] function isStaticPath(req) { const url = req.url for (let i = 0; i < staticPaths.length; i++) { if (url.match(new RegExp(`^${staticPaths[i]}`))) { return true } } return false } const match = require('fs-router')(__dirname + '/routes', { filter: (f) => { let p = f.slice(__dirname.length + "/routes".length) return !isStaticPath({ url: p }) } }) module.exports = async function(req, res) { if (isStaticPath(req)) return await files(req, res, { public: 'routes' }) let matched = match(req) if (matched) return await matched(req, res) if (req.url === '/robots.txt') return send(res, 200, robotsTxt) return send(res, 404, { error: 'Not found' }) }