0

What I want to do is check the script URL for a parameter & display the content of that parameter like:

www.mywebsite.com/mynodescript.js?parameter=i+am+new+to+node!

Now I want to display "I am new to node!" on browser screen and if the parameter is not present I just want to exit.

edit: I found this code but I am not sure how to deploy it

var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;

Note: i want to upload my script on heroku & want it to call it remotely

golev
  • 41
  • 5
  • Also see this question which is very similar: https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js – Senica Gonzalez Feb 09 '18 at 20:23

1 Answers1

1

When you say you don't know how to deploy it, I'm assuming you don't have a http server setup yet?

Look at using Express (http://expressjs.com/). It's easy enough to get started with.

Create a file called app.js like this:

const express = require('express')
const app = express()

// This handles the path /mynodescript.js You can create a bunch of functions like this to handle different paths. See the express docs for more.
app.get('/mynodescript.js', (req, res)=>{
  let parameter = req.query.parameter; // <-- Could also do let {parameter} = req.query This is where you would pull out your url parameters
  if(parameter){
    res.send(parameter); // <-- this sends it back to the browser.
  }else{
    res.status(422).end(); // <-- you can set a status here or send an error message or something useful.
  }
})

app.listen(3000, () => console.log('Example app listening on port 3000!'))

Start the script using node app.js from the same directory.

Then open a browser and go to http://localhost:3000/mynodescript.js?parameter=i+am+new+to+node and you should see your parameter

Note that you will have to install express first npm install express --save

Note that you do not have to use express. There are quite a few http libraries available for nodejs. Or you can use the built-in http server (https://nodejs.org/api/http.html). It's good to get familiar with the NodeJS docs, but their http server is cumbersome to work with.

Senica Gonzalez
  • 7,996
  • 16
  • 66
  • 108
  • thanks :) it worked when i tried it locally but when i uploaded it on a server of heroku it is crashing i want to host it online so that i access it with browser – golev Feb 09 '18 at 21:42
  • what i want is something like this [code] const express = require('express') const app = express() parameter = req.query.parameter; if(parameter){ res.send(parameter); }else{ res.status(422).end(); } [/code] but it is not working can you fix it ? the server i am hosting script on will make script run on server so i just want to check for requested url – golev Feb 09 '18 at 22:03
  • if you are deploying to heroku do not provide the port, instead use **process.env.PORT** – Manish Kumar Feb 10 '18 at 07:42
  • thanks it really helped i just read some docs & achieved what i wanted :D – golev Feb 11 '18 at 17:48