-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (28 loc) · 1004 Bytes
/
index.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
28
29
30
31
32
const connect = require('connect')
const http = require('http')
const mustache = require('mustache')
const path = require('path')
const qs = require('qs')
const serveStatic = require('serve-static')
const url = require('url')
const { name, description, author } = require('./package.json')
const { readFile } = require('fs').promises
module.exports = async function () {
const app = connect()
const indexTemplate = await readFile(path.join(__dirname, 'src', 'mustache', 'index.mustache'), 'utf-8')
app.use(serveStatic(path.join(__dirname, 'src', 'css')))
app.use(serveStatic(path.join(__dirname, 'src', 'static')))
app.use('/', (req, res) => {
const { query } = url.parse(req.url)
let text = decodeURIComponent(query)
if (!text || text === 'null') text = 'Put text here like this: `/?hello world!`'
const index = mustache.render(indexTemplate, {
author,
description,
text,
title: name
})
res.end(index)
})
return http.createServer(app)
}