23 lines
631 B
JavaScript
23 lines
631 B
JavaScript
const { send } = require('micro')
|
|
const files = require('serve-handler')
|
|
const match = require('fs-router')(__dirname + '/routes')
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
return send(res, 404, { error: 'Not found' })
|
|
}
|