-1

I have a Node.js project in PhpStorm that runs the index.js file just fine when I click the Run command, but now I need to run that with query strings in the URL (e.g. index.js?q=query) and get the query string parameter using something like req.query in Express. Problem is I am new to PhpStorm and cannot figure out how to run the scrips with parameters in the URL.

So far I only see the option to run the index.js file but no option to run index.js?q=query. Normally I would just load the script in a browser but that kind of defeats the purpose of using a headless browser and it would require me to copy the application from the PhpStorm project file to a directory in XAMPP.

If I run a command like node index.js it works fine but if I change that to node index.js?q=query I get an error saying "Error: Cannot find module"

I am also not sure if I am using the right syntax for accessing the query parameters via Express.

const express = require('express')
const exp = express();
const url = exp.req.query.url;
const keyword = exp.req.query.q;
const source = exp.req.query.source;
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • PHP Storm is an odd choice of IDE for writing Node.js code with, but it doesn't have to be used to write PHP. – Quentin Feb 19 '21 at 11:48
  • 2
    @Quentin [PhpStorm = WebStorm + PHP + DB](https://stackoverflow.com/a/25647482/783119). Why it's odd (to use an IDE that supports more than one language), even if it's primarily focused on another lang (but fully supports the required language anyway)? – LazyOne Feb 19 '21 at 12:53
  • PhpStorm allows people to create node.js projects just like they can create a php project. – PostAlmostAnything Feb 21 '21 at 00:20

1 Answers1

1

Normally I would just load the script in a browser

While the JavaScript programming language got its start as a client-side language, it is a general purpose language.

If you are using the express module, then it replaces your webserver and other server side language. It can't run inside the browser.

(Note that in your example code, you never start the Express server. See the Hello World example in Express' documentation).

You can't run your script and provide it with a URL as one operation. You need two operations:

  1. Run your script to start the server
  2. Issue an HTTP request to it (for example via a browser, curl, or another script).

If you wanted to write unit tests for an Express server then that gets into fairly complex territory with lots of options. See this page for example.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335