How to link standalone nodejs files to main nodejs server? - node.js

I am creating a node.js project with the ability to upload files and access a mysql database. The functions are finished and work as standalone files. So far I am able to access different html pages but I'm not sure how to include the nodejs upload and access functions. The functions will be used on different pages than the home page. All of the tutorials I've found establish a server, create the function and everything else all in one file. I'm assuming this is strictly for learning purposes.

After several hours of researching the structure of node.js, I realized the format of node.js projects is different than other languages. Modules are created, then imported into main to be called when necessary. I was expecting each page to have their own module imports declared in a separate page.

Related

Is it possible to have MVC in sub folders when using node.js express framework?

Is it possible to have MVC in sub folders (modules) when using node.js express framework?
Today I have this structure of my project:
Controllers
Blog/*
Poll/*
And many more
Models
Blog/*
Poll/*
And many more
View
Blog/*
Poll/*
And many more
Routes
Blog
Poll
And many more
Should it be possible to have this structure in a module way instead?:
Blog
Controllers/*
Models/*
View/*
routes.js
Poll
Controllers/*
Models/*
View/*
routes.js
And more modules
When I develop I feel I mostly work with one part of the project at the time. When I the current structure its a lot of moving between folders. However most of the time I only focus on "module". It will also be easier to remove as module just to delete the sub folder and all it files are removed. If a project has more 30 modules it will be hard to find the file (Its possible to search but should be nice just have it in same folder). I been working with Drupal before that has this type of module system where you can easy add and remove modules (Might be that am used to to it and not yet familiar with this new this way of doing it). What are the main benefits of the first approach over the module way?
Is this possible to achieve in the express framework? If so any tutorials on how to set it up? Any other node.js framework support this?

How to use JavaScript for front-end and Nodejs on backend

I have looked high and low and can’t find anything. I’m a little confused on Web Developement with Nodejs. I have built a simple static file server with pure Nodejs without any framework based on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework. I’m able to serve my static .html files and .css files, but I can’t seem to figure out how to serve the .js files. Whenever I include my front-end JavaScript files in my server.js files I get errors like window is undefined or document is undefined. How does one go about developing dynamic websites with Nodesjs. I’m used to the PHP way of development. I just need help getting started with Node.js.

How to create multiple page app with vue-cli(webpack) (with different router for each one, and different dist/index.html)

I want to serve different vue pages from server by different routes. For now I'm using only one route (/static) to serve vue files for one "page" that contains vue-router.
I can't go one like this because the application should use cookies to verify access rights to different pages.
How can I accomplish this without using browser cache ?
Multi-Page apps are now supported very well with vue-cli v3:
https://cli.vuejs.org/guide/html-and-static-assets.html#building-a-multi-page-app
Found the solution for me. Unfortunately not a build in one. I just ran build process for two different builds with two different webpack configs which in their turn have different entry and output properties. Just in case someones find this helpful.

Connecting React and React Native with Express

So I just started learning ReactJS and React Native.
I have some knowledge of MEN (Mongo, Express, Node). Up to this point, I learned how to res.render() files and pass objects in there.
Now what I need to do, is make MERN app. This app also needs to have Android and iOS version of it.
So far I learned that R stands for ReactJS, not react-native. Is there a way so it includes both? And where do I put react files when I have folder structure like from express-generator? Or is there a way they can be in completely different directories, and one calls the other via import?
It comes down to architecture I believe. The way I like to create the stack goes as follows.
You can create an API using Mongo/Express/Node that serves endpoints for your client app (created using reactjs, react-native and whatever other tech you want to include) to call using HTML requests. This would work for both mobile apps (using react-native) and desktop apps (using reactjs).
There's a couple different ways to deploy this. You can create 2 separate apps, a server app and a client app, which are both hosted individually by 2 separate hosts. This is useful because you decouple your front end code from your back end code. Also, you can have 2 separate directories for your code.
Another method of deployment would be to have your server serve your client files. This ones a little bit tricker, but you will be able to deploy your entire app inside 1 host so this option is also cheaper. I would suggest reading this article to figure out how to implement this and the file structure https://originmaster.com/running-create-react-app-and-express-crae-on-heroku-c39a39fe7851

Making files not accessible in node js

I'm using WebStrom to create a node js website.
1) What template of project should I choose (AngularJS, node express etc)? I do use angular-js but I also need a server side.
2) What are the best practices for the project structure?
3) How I make the server side js files not accessible from the browser? I created a few projects from the different templates and I managed to reach every single js file by writing its path in the browser.
Any help will be profoundly appreciated!
I would suggest using templating. I personally like using handlebars but there are many different options out there.
https://github.com/fixiecoder/node-express-handlebars-boilerplate
This is a link to a very basic node express application that uses handlebars templates. There are also many tutorials that you can use to learn more.
The line app.use(express.static(__dirname + '/public')); in the server.js file is what is used to specify a public folder that is the only thing that the browser will be able to access. Everything outside of this is private to the server.
The best thing you could do though it just do some basic tutorials from start to finish to understand how it all fits together.

Resources