Check out https://github.com/newagesoftwareLLC/Node_Message_Server if you want to use WebSocket to notify all web app users that there's new data available and to update or add this new data to a DOM element.
Last active
March 25, 2021 14:47
-
-
Save erfg12/e971095e90bed735201f46af2a4c9ab7 to your computer and use it in GitHub Desktop.
Simple Node.JS Express post parsing without Body-Parser.
This file contains hidden or 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
| // 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); | |
| }) |
This file contains hidden or 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
| <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