mxn-body-parser

Asynchronous HTTP request body parser

Usage no npm install needed!

<script type="module">
  import mxnBodyParser from 'https://cdn.skypack.dev/mxn-body-parser';
</script>

README

mxn-body-parser

Asynchronous HTTP request body parser

Install

$ npm install mxn-body-parser

Usage

Example:

// HTTP server
const http  = require("http");

// MXN Connect Framework and Middleware
const connect = require("mxn-connect");

// Handling Request Body
const getRequestBody = require("mxn-body-parser");

// Instantiating the App
const app = connect();

app.use("/api", async function(req, res, next) {
    // Check if method is "POST"
    if (req.method !== "POST") {
        res.statusCode = 405; // Method not allowed
        res.setHeader("Allow", "POST");
        res.setHeader("Content-Length", 0);
        res.end();
        return;
    }
    
    // Get Request Body
    const body = await getRequestBody(req);
    console.log(body);
    
    // Proceed further with Request body
    ...
});

// Create node.js HTTP server and listen on port
const options = { };
const server = http.createServer(options, app).listen(3000, function() {
    console.log("MXN Connect server is running on port " + 3000);
});

server.on("error", function(error) {
    console.error("Error event handler called: " + error);
});

License

This module is released under the MIT license.

Related

  • mxn-connect - High performance middleware framework based on connect
  • mxn-favicons - Serve site icons (favicon and apple-touch-icon) from any directory