zweizhao.github.io

项目,文章,随笔,博客,Markdown,个人网站,人生感悟。

View on GitHub

require('coffeescript/register)'

入口文件引入即可

简单模板

main.js

require('coffeescript/register')

let Main = require('./main.coffee')

new Main()

main.coffee

app = (require "express")()
bodyParser = require "body-parser"

module.exports = class
    constructor: ->
        app.use bodyParser.urlencoded { extended: false }
        this.start()
    start: ->
        # 跨域设置
        app.all '*', (req, res, next) ->
            res.header "Access-Control-Allow-Credentials", true
            res.header "Access-Control-Allow-Origin", "*"
            res.header "Access-Control-Allow-Headers", "X-Requested-With"
            res.header "Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"
            res.header "X-Powered-By", ' 3.2.1'
            res.header "Content-Type", "application/json;charset=utf-8"
            next()

        app.get '/', (req, res) ->
            res.end JSON.stringify {
                a: 'a'
                b: 'b'
            }

        app.post '/login', (req, res) ->
            username = req.body.username
            password = req.body.password
            res.end 'username=#{username} password#{password}'

        app.listen 8080, "0.0.0.0", ->
            console.log "run in http://0.0.0.0:8080"