Skip to content

Node.js

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Feb 6, 2018 · 8 revisions

Debug

  • npm install -g node-inspector
  • node-debug bin/www

Simple request-echo server

  • npm install express
  • node ./app.js
// app.js
const express = require('express')
const bodyParser = require('body-parser')
const app = express()

var options = {
    inflate: true,
    limit: '100kb',
    type: '*/*'
}

// app.use(bodyParser.text(options));
app.use(bodyParser.raw(options));
// app.use(bodyParser.json())
// app.use(bodyParser.urlencoded({ extended: false }))

app.get(
    '/*',
    function(req, res) {
        var output = '== GET == ' + new Date() + '\n' +
            'URL: ' + req.originalUrl + '\n' +
            'QUERY: ' + JSON.stringify(req.query) + '\n' +
            'BODY:\n' + req.body.toString()
            // 'BODY: ' + JSON.stringify(req.body)

        console.log(output + '\n')
        res.send(output)
    }
)

app.post(
    '/*',
    function(req, res) {
        var output = '== POST == ' + new Date() + '\n' +
            'URL: ' + req.originalUrl + '\n' +
            'QUERY: ' + JSON.stringify(req.query) + '\n' +
            'BODY:\n' + req.body.toString()
            // 'BODY: ' + JSON.stringify(req.body)

        console.log(output + '\n')
        res.send(output)
    }
)

app.listen(3000, () => console.log('Example app listening on port 3000!'))

Clone this wiki locally