I am learning about nodejs and expressjs. I create routes and controllers: site, news & inputenter.
image
But when i save and use npm start(nodemon), only news & site is load.
When i Inspect and view the sources, my inputgroup.js & InputGroupController.js file didn’t upload.
image
image
How can i fix this?
I tried clearing the cache, save all the file, restart the server, f5 and still not working
2
Answers
This is because NodeJS and ExpressJS are supposed to be used in the backend, and the whole code is never exposed to the frontend. You should not be worried about it.
Hopefully this fills in some high level things for you.
So your Express/Node Application is what is called a "Web Server" as in it should be ran as a backend application which returns information to the client. The fact you can see the raw JS files in the browser means that you’re uploading them as if they were a frontend client application. This really isn’t what you want to do.
Typically what you want to do with express is open a seperate terminal or cmd line window and run
npm run start
ornpm start
and then in your browser navigate to wherever the server is running likehttp:localhost:3000/
which is the typical default server path, although you may have set the port differently in your application, so it could behttp://localhost:3001/
or a different port.When you make a HTTP request to a path on your server like
GET
http://localhost:3001/mycoolpath
if there is a controller specified for that route it will take the request in, process it and then return a result, likely some JSON or HTML data.Typically then a frontend application will interperate this information and produce a change in the client’s view.
You can write some raw HTML and JS to make a call to your web server and see the results, or use a larger framework to do this for you like React, Vue or Angular.