-3

How do I determine and change both the "DocumentRoot" equivalent (of Apache) and port number on Node.js? I need to test a script file by calling the function and passing some parameters (yes, I know the file can execute it automatically).

There is no "getting started" or mention of this in the documentation.

John
  • 1
  • 13
  • 98
  • 177

2 Answers2

3

Apache HTTPD is a generic web server. Node.js is a development framework that includes a standard library for creating HTTP servers. Thus, there isn't a standard configuration for a Node.js based application like there is for Apache HTTPD.

The basic example of writing a web server with Node.js is found at https://nodejs.org/api/synopsis.html#synopsis_example

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
}); 

Which is to say, you define where files are loaded from and what port they are served over.

This is where frameworks like Fastify, Hapi, and Express come in. The make it easier to write generic web servers.

James Sumners
  • 14,485
  • 10
  • 59
  • 77
  • A friend advised Express and I followed the directions here: https://coderwall.com/p/mbov6w/running-nodejs-and-express-on-windows however `C:\MEDIA\INTERNET\Node.js>node app module.js:540 throw err; Error: Cannot find module`. I'm not interested in setting up a VM, I want to run it locally on Windows. What part of that tutorial is missing? – John Feb 20 '18 at 19:44
  • Ah: `node "C:\Users\example\index.js"` ... so how do I execute a function named `callWeatherApi(city, date)` then? I can fill my own parameters. That is really what I'm ultimately asking though I try to keep things fairly simple since I knew it would not be as straight-forward as Apache. – John Feb 20 '18 at 19:52
  • Apache HTTPD is an application not a development framework. They are not equivalent. Use https://nodeschool.io/ to learn Node.js. – James Sumners Feb 20 '18 at 20:13
  • While what you posted helps it skips some steps so I will up-vote it and accept the answer I posted myself. I think that is fair, granted it was a difficult to ask question to begin with. Thank you. – John Feb 21 '18 at 01:20
  • You changed the question after it had been answered. One cannot accomplish a goal when it keeps moving. Testing should be done with a library like http://www.node-tap.org/ – James Sumners Feb 21 '18 at 13:38
-1

I first installed Node.js and it just has a command prompt in Windows. Still not sure the port number.

Node seems to execute scripts from it's directory (e.g. C:\Node.js\). As James mentioned in another answer Node.js doesn't do much on it's own. I followed a tutorial on getting Express to work on Windows. The tutorial failed to mention where scripts are run from so ignore the directions past running the following on the normal command prompt (not Node's console):

  1. Run npm install
  2. npm install express -g
  3. npm install url -g
  4. npm install fresh -g
  5. npm install cookie -g
  6. npm install methods -g
  7. npm install crc -g
  8. npm install send -g
  9. npm install connect -g
  10. npm install commander -g
  11. npm i -D run-func

Okay, the last line of code allows us to run a function and pass parameters which I found via Pawel's answer here.

So now I can execute the following:

node run-func "C:\Users\John\HTTP\index.js" function_name param1 param2
John
  • 1
  • 13
  • 98
  • 177
  • Installing these modules globally is _not_ a good practice. Modules should be installed local to the project they apply to. – James Sumners Feb 21 '18 at 13:37