简体   繁体   中英

Using a sh script to send node js server requests with specified query parameters on the URL

I'm a little lost right now on how to get started on my current project. I really just need some help at just getting it going and I should be able to take it from there. I have some experience with JavaScript, php, and mysql but almost nothing in node js.

I have a.sh script file that looks something like this:

curl localhost:3000/signup?user=jack\&height=6\&time=0
curl localhost:3000/arm?left=0.000000\&right=0.000000\&time=0 --cookie "USER=jack"
curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=jack"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=jack"
curl localhost:3000/other?ir=0\&time=30 --cookie "USER=jack"

I want to send my node js server requests with these specified query parameters in the shell but I have no clue how to do so. Here is my node js code thus far:

const { exec } = require('child_process');
var script = exec('myscript.sh' ,
        (error, stout, stderr) => {
           console.log(stdout);
           console.log(stderr);
           if ( error !== null ) {
              console.log(`exec error: ${error}`);
            }
        });

var http = require('http');
var url = require('url');

http.createServer(function(req, res) {
          var q = url.parse(req.url, true).query;
          res.writeHead(200, {'Content-Type' : 'text/html'});
          res.end("user " + q.first + " height " + q.second);
       }).listen(3000);

When I go to localhost:3000 I can see "user undefined height undefined" and when I type "node file.js" into my command line then I get the script to start echoing each line in the linux command line... But how do I put the two together? How would I parse a script or use that script to send the requests that I need to my node js server? I really just have no idea where to begin on this project. Would things be easier simply using php and mysql? Thanks for any help I can get with this. Thanks.

You have to wait for your http server before your can make requests to it:


const http = require('http');
const url = require('url');
const { exec } = require('child_process');


// create http server
const server = http.createServer(function (req, res) {

    // parse query parameter
    var q = url.parse(req.url, true).query;

    // write http header to client
    res.writeHead(200, {
        'Content-Type': 'text/html'
    });

    // send back body repsonse
    res.end("user " + q.first + " height " + q.second);

});


// wait for the http server to start
server.on("listening", () => {

    // do requests to the defined http server above
    const script = exec('myscript.sh', (error, stout, stderr) => {

        console.log(stdout);
        console.log(stderr);

        if (error) {
            console.log(`exec error: ${error}`);
        }

    });

});

server.listen(3000);

Dont forget to make your.sh script executable chmod +x myscript.sh

#!/bin/bash
curl "http://localhost:3000/signup?user=jack&height=6&time=0"
curl "http://localhost:3000/arm?left=0.000000&right=0.000000&time=0" --cookie "USER=jack"
curl "http://localhost:3000/echo?dist=9.220000&time=10" --cookie "USER=jack"
curl "http://localhost:3000/line?l1=1&l2=1&l3=1&time=20" --cookie "USER=jack"
curl "http://localhost:3000/other?ir=0&time=30" --cookie "USER=jack"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM