Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple put request for nodejs without express

Tags:

node.js

I need to be able to upload a zip file to a brightsign unit and thinking of creating a rest api which I can make a put request to send the zip file.

But the problem is that all examples I find is using frameworks such as express. Is it possible to make a rest API handling a PUT requst in nodejs without using extra frameworks?

The problem is that I can only use modules that does not need a "configuration" step on the brightsign player. So I can use modules that only contain plain javascript (I hope my explanation makes sense).

like image 326
user1716970 Avatar asked Dec 10 '25 03:12

user1716970


1 Answers

Vanilla (NodeJS HTTP API)

You will need to listen for the PUT somehow :

const http = require('http');
const querystring = require('querystring');
const server = http.createServer().listen(3000);

server.on('request', function (req, res) {
    if (req.method == 'PUT') { //PUT Only
        var body = '';
        req.on('data', function (data){body += data;});
        req.on('end', function () {
            var PUT = querystring.parse(body);
            console.log(PUT);
            res.writeHead(200, {'Content-Type': 'text/plain'})
            res.write('\n[request received successfully]\n'); res.end();
        });
    }else{res.end();}
});

You can test it out using curl :

curl -X PUT -d destPath=/home/usr/images -d fileName=selfie.png -d fileData=base64string localhost:3000

In express is much simpler, comment if you need an express example

like image 120
EMX Avatar answered Dec 11 '25 18:12

EMX