Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active March 25, 2021 14:47
Show Gist options
  • Save erfg12/e971095e90bed735201f46af2a4c9ab7 to your computer and use it in GitHub Desktop.
Save erfg12/e971095e90bed735201f46af2a4c9ab7 to your computer and use it in GitHub Desktop.
Simple Node.JS Express post parsing without Body-Parser.
// Post data to `http://(host):(port)/handle` in JSON format.
var express = require("express");
//var WebSocket = require('ws');
//var sqlite3 = require("sqlite3").verbose();
var app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }))
// Can change path to '/node/api/handle' to support iisnode.
// If you do, then navigation would be http://(host):(port)/api/handle
app.post('/handle',(req,res) => {
console.log(req.body);
// websocket example if you want to let other web apps know new data has come through
/*var ws = new WebSocket('ws://localhost:9898/inspection');
ws.on('open', function open() {
ws.send(JSON.stringify(req.body));
console.log("ws data sent.");
});*/
// store post data into an SQLite database file
/*let db = new sqlite3.Database("db.db", (err) => {
if (err) {
console.error(err.message); // this will stop execution of code block
} else {
db.run("INSERT INTO inspection (title, message) VALUES (?, ?)", [req.body.title, req.body.message, function (err, result) {
if (err) {
console.error(err.message);
} else {
console.log("db data entered.");
}
});
}
});*/
res.send("handled");
});
app.listen(process.env.PORT,() => { // can assign a static port here.
console.log("Started on PORT " + process.env.PORT);
})
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="node_express_post.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="/">
<match url="/*" />
<action type="Rewrite" url="node_express_post.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment