2

I've just setup a Virtual Machine that runs Ubuntu + NodeJS. I'm new to Linux and i know how to execute my code from the terminal.

node app.js

But how do i tell my server that i want to run "thisfile.js" in the background like PHP does? I know that javascript is a frontend language, that runs inside a browser. I also know that nodeJS is a Javascript environment that uses the Googles V8 Engine. But how do i tell NodeJS to not pass "thisfile.js" to the visitors browser? I'm sorry but i don't fully understand NodeJS, I'm making my first steps right now.

Installing NodeJs + Express on ubuntu + starting the localhost

User121018
  • 23
  • 5
  • Node.js doesn't automatically run on any browser, you don't need to do any special steps to prevent it from running in a browser. You can definitely run it on your local machine without any internet connection – Blundering Philosopher Oct 14 '18 at 07:30
  • I'm using also a http-server, should i keep the js files that are meant for Nodejs outside the http-server folder? – User121018 Oct 14 '18 at 07:34

2 Answers2

1

NodeJS is a tool for writing server-side javascript. No file or information gets to passed visitors attempting to connect to your server unless you specifically write code that does so.

It seems you don't really understand what NodeJS does, which is totally ok, and I would recommend following a basic tutorial that explains what NodeJS is and how to use it, such as: https://www.w3schools.com/nodejs/nodejs_intro.asp

0

When you run your node application, the backend JS logic files will be compiled into machine code by V8 engine. Hence it will not pass any logic base file to the visitors browser.

Assuming you're using the default express application structure, the only place for you to pass public resources file like css/html/js/images is the public folder in the default express app.

├── app.js
├── bin
│   └── www
├── package.json
├── public //Public files that browser will have access to is place inside here
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes // browser will not have access, as it's compiled into machine code
│   ├── index.js
│   └── users.js
└── views
    ├── error.pug
    ├── index.pug
    └── layout.pug
Marcia Ong
  • 781
  • 8
  • 15
  • Do i need to create the whole structure myself or can i find this somewhere inside the node_modules folder? Sorry if i ask stupid questions. – User121018 Oct 14 '18 at 07:46
  • No you do not need to create the structure yourself. You can generate it using just one line of code. https://expressjs.com/en/starter/generator.html – Marcia Ong Oct 14 '18 at 07:48
  • I would advice you, to spend sometime reading up on the getting start guide from expressjs.com – Marcia Ong Oct 14 '18 at 07:51
  • 1
    I found an answer thru google! If you wouldn't mention express i wouldn't know what to search for. Thank you. – User121018 Oct 14 '18 at 07:52
  • Welcome, have fun coding – Marcia Ong Oct 14 '18 at 07:57
  • Could you give me also a small hint how to access the public folder from the browser? Do i need to run http-server on the public folder? – User121018 Oct 14 '18 at 08:03
  • For example you have 123.jpg in the root folder in public folder. You start your node instance for example at port 8080. You can reach the image by calling localhost:8080/123.jpg – Marcia Ong Oct 14 '18 at 08:06
  • Thanks, i found a solution i was searching for. Stackoverflow is really helpful, thank you. https://stackoverflow.com/a/23122981/10492820 – User121018 Oct 14 '18 at 08:15
  • Sure no problem, just note that there's a difference between nodejs app and express node framework. I would recommend you to learn express framework as lots of things are already been done for us, thus we dun need to reinvent the wheel using plain nodejs app – Marcia Ong Oct 14 '18 at 08:20
  • I closed my VM and turn off my PC, today is the next day and nothing works. I can run scripts using node test.js and it works. But i can't run http-server, it says "command not found". – User121018 Oct 15 '18 at 05:32
  • You need a script to keep your server running. I keep my express node application running in vm using forever script. https://www.npmjs.com/package/forever – Marcia Ong Oct 15 '18 at 06:04