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

2
  • 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 Commented Oct 14, 2018 at 7:30
  • I'm using also a http-server, should i keep the js files that are meant for Nodejs outside the http-server folder? Commented Oct 14, 2018 at 7:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

11 Comments

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.
No you do not need to create the structure yourself. You can generate it using just one line of code. expressjs.com/en/starter/generator.html
I would advice you, to spend sometime reading up on the getting start guide from expressjs.com
I found an answer thru google! If you wouldn't mention express i wouldn't know what to search for. Thank you.
Welcome, have fun coding
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.