forked from ded/jade-server
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpugserver.js
27 lines (23 loc) · 855 Bytes
/
pugserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env node
const http = require('http')
const fs = require('fs')
const path = require('path')
const pug = require('pug')
const staticfiles = require('node-static')
const pugPath = process.argv[process.argv.length - 1]
const port = process.argv[process.argv.findIndex(e => e === '-p') + 1] || 8080
const fileServer = new staticfiles.Server(pugPath || '.')
try {
http.createServer((req, res) => {
console.info(req.method, req.url)
const requrl = req.url.match(/\/$/) ? `${req.url}index` : req.url
if (fs.existsSync(path.join(pugPath, `${requrl}.pug`))) {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(pug.renderFile(path.join(pugPath, `${requrl}.pug`)))
} else {
req.addListener('end', () => { fileServer.serve(req, res) }).resume()
}
}).listen(port)
} catch (e) {
throw new Error(e)
}