Created
October 8, 2023 01:12
-
-
Save audinue/8cd1e4bd46bfa677fd11db10d3278cff to your computer and use it in GitHub Desktop.
PUG + Stylus + CoffeeScript server
This file contains 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
port = 3000 | |
express = require 'express' | |
serveStatic = require 'serve-static' | |
serveIndex = require 'serve-index' | |
router = require './router' | |
express() | |
.use router | |
.use serveStatic('.') | |
.use serveIndex('.') | |
.listen port, () -> | |
console.log "Server is listening on #{port}" |
This file contains 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
{ | |
"name": "fun", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"dev": "coffee index.coffee" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"express": "^4.18.2", | |
"jstransformer": "^1.0.0", | |
"jstransformer-coffee-script": "^0.0.2", | |
"jstransformer-pug": "^0.4.0", | |
"jstransformer-stylus": "^1.5.0", | |
"serve-index": "^1.9.1", | |
"serve-static": "^1.15.0" | |
} | |
} |
This file contains 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
express = require 'express' | |
fs = require 'fs/promises' | |
pug = require('jstransformer') require('jstransformer-pug') | |
stylus = require('jstransformer') require('jstransformer-stylus') | |
coffeeScript = require('jstransformer') require('jstransformer-coffee-script') | |
router = express.Router() | |
router.get '*', (req, res, next) -> | |
routes = [ | |
[ | |
/\.pug$/ | |
(string) -> res.send pug.render(string).body | |
] | |
[ | |
/\.stylus$/ | |
(string) -> res.type('css').send stylus.render(string).body | |
] | |
[ | |
/\.coffee$/ | |
(string) -> res.type('js').send coffeeScript.render(string).body | |
] | |
] | |
reject = (error) -> res.type('text').send error.message | |
file = req.path.substring(1) | |
if file is '' | |
file = 'index.pug' | |
route = routes.find ([regex]) -> regex.test file | |
if route | |
[_, resolve] = route | |
fs.readFile file, 'utf8' | |
.then resolve | |
.catch reject | |
else | |
next() | |
module.exports = router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment