What are the benefits of using Express in NodeJS to run both a backend and a frontend server simultaneously?

My current project involves a simple NodeJS/AngularJS application structured as follows:

/frontend/index.html <-- AngularJS home page
/frontend/js/app.js  <-- AngularJS app.js
/backend/package.json<-- NodeJS package.json
/backend/index.js    <-- NodeJS script

Currently, I am serving the backend API using my NodeJS express server, while accessing the frontend directly via file://.../frontend/index.html in my browser.

I'm wondering if it is considered good practice to have both the "Backend API" and the frontend served by NodeJS (potentially using two express instances). If so, how should I organize the code for this setup?

Answer №1

When I access the frontend part directly (file://.../frontend/index.html) in my browser.

This method is only applicable on your personal computer and cannot be used for a typical web server that others can access. Therefore, this is not considered a suitable practice for a standard web server intended for public use on the internet.

By simply adding one line of code utilizing the express.static() middleware, you can configure your existing node.js server to serve all static HTML files without requiring a separate process. While larger-scale operations may implement an NGINX proxy to handle static HTML file serving, it appears unnecessary for your current needs. For now, utilize express.static() within your existing node.js server to serve static files and eliminate any reliance on file:// references.

Is it beneficial to run two separate NodeJS instances for serving both the "Backend API" and the frontend, and how should I structure the code?

There seems to be no justification for running two node.js instances based on the information provided. It is recommended to operate a single instance responsible for serving both the API and static HTML files within your application.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

enigmatic issue arising in Angular/Node

Could someone please shed some light on what might be causing the issue in my code? Thank you in advance! webpack: Compilation Failed. My Angular CLI: 1.6.3 Node: 8.9.4 OS: Windows 32-bit Angular: 5.2.1 Error Message: ERROR in ./node_modules/css-loader ...

Are there any alternatives in NodeJS similar to PyAutoGUI that have the ability to search for an image within a screenshot?

I've been working on developing a bot to automate tasks in a video game using JS and Node, primarily utilizing RobotJS. However, I'm encountering an issue where I need to locate and click on a moving element on the screen, similar to PyAutoGUI&ap ...

Utilize NodeJS to dynamically alter the text outputted on an HTML page

For educational purposes, I am designing a website where users can sign in by entering their name on the login page. After signing in, they will be redirected to the home page which displays a personalized welcome message using their name. I have included ...

Exploring the use of Node.js exclusive modules in Docker containers

I have a nodejs project that includes references to a module I developed and hosted in a private Github repository. The dependencies listed in the package.json file are as follows: "dependencies": { ... other stuff ... "my_module": "git+https://gi ...

The functionality of Ng-Show is acting up

$scope.stay = function() { alert("Inside Keep me In") $scope.timed = false; $scope.isLogStatus = true; } $scope.displayAlert = function() { $scope.timed = true; alert("inside display") } function idleTimer() { var t; $window.o ...

Issue: $controller:ctrlreg The controller named 'HeaderCntrl' has not been properly registered

I am encountering an error while working on my AngularJS program. I have defined the controller in a separate file but it keeps saying that the controller is not registered. Can someone please help me understand why this issue is happening? <html n ...

Issue with displaying SequelizeInstance object in Express Handlebars template (NodeJS, Express)

When using NodeJS, Express, and Sequelize, I am encountering an issue where my view displays [object SequelizeInstance:Todo] instead of the actual data instance value when called with res.render(). However, when called by .send (see commented line below), ...

Typescript versus ES5: A comparison of Node.js server-side applications written in different languages

Note: When I mention regular JavaScript, I am referring to the ES5 version of JS. As I lay down the groundwork for a new project, my chosen tech stack consists of Node.js for the back-end with Angular2 for the front-end/client-side, and Gulp as the build ...

Updating the DOM does not occur by simply adding an object to the Array; instead, the database is updated once the data has

My database has verified data that is being updated, however, the DOM is not reflecting these updates. <ul> <li ng-repeat="aReview in reviewList"> .... .... </li> </ul> <script> if(globalMethods.stringVa ...

Issues with Braintree webhooks and CSRF protection causing malfunction

I have successfully set up recurring payments with Braintree and everything is functioning properly. Below is an example of my code: app.post("/create_customer", function (req, res) { var customerRequest = { firstName: req.body.first_name, lastN ...

What is the best way to make gulp-vulcanize exclude the socket.io.js file from processing?

One of my HTML files includes a reference to /socket.io/socket.io.js, and I want to vulcanize this file while ignoring that particular script tag. Here is the gulp task I created for this purpose: // Vulcanize HTML files var vulcanizeHtmlSrc = 'view ...

Exploring the Possibilities of Using Multiple View Directories in Koa

In my NodeJS & Koa-views web app, I am organizing it in a modular way with the following folder structure: A/ routes/ models/ views/ B/ routes/ models/ views/ Within my Index.js file, I have the following code snippet: app.use(views(`${__dirname}/A ...

Sending data with React using POST request

Currently in my React application, I have a form that includes fields for username and password (with plans to add "confirm password" as well). When submitting the form, I need it to send JSON data containing the email and password in its body. The passwo ...

Issue with fs.createReadStream function: it is not recognized as a valid function

I'm currently developing a project in VUE that utilizes 'fs', and I have included my code below. async Upload(file){ let fs = require('fs'); console.log(file); console.log(this.dialogImageUrl); ...

Update a specific HTML element when the result of an equation is modified

I need assistance with my angularjs website development project. I have implemented content-specific tabs, each with its own unique content. When a tab is selected, only the corresponding content should be displayed. The tabs are assigned IDs in the contro ...

My goal is to eliminate the console error message "@ Angular: GET http://localhost:4200/assets/i18n/1/fr.json 404 (Not Found)" related to missing file

Is there a way to prevent the "GET http://localhost:4200/assets/i18n/1/fr.json 404 (Not Found)" error from appearing in both the console and network of the browser while using Angular? I need a solution for this.custom.translate.loader.ts****** return Obse ...

AngularJS does not refresh the DOM as anticipated

I have a fiddle demonstrating the geocoding of an address input into a textbox. The issue I'm facing is that after pressing 'enter', the table does not update immediately; it waits for another change in the textbox. How can I make it update ...

Derby.js: organizing client-side code into separate files

I'm attempting to break up some client-side code into separate files within a Derby.js project. This code must remain client-side as it interacts with the TinyMCE editor. My initial attempt was: app.ready(function(model) { var tiny = derby.use(re ...

Updating an Array inside an Object using NodeJS and MongoDB

Trying to make changes in an array nested within an object on my NodeJS/Express application. Here is the information extracted from the Locations collection: { _id"56873fc9182b3741059357d0", longitude: 113.83507800000007, latitude: 22.1533884, lo ...

`npm security check reveals a potential vulnerability related to Inefficient Regular Expression Complexity. For more information, refer to: https://github.com/advisories/GHSA-9vvw-cc

# npm audit report debug <3.1.0 debug Inefficient Regular Expression Complexity vulnerability - https://github.com/advisories/GHSA-9vvw-cc9w-f27h No solution currently available node_modules/body-parser/node_modules/debug node_modules/express/node_modu ...