Tips for structuring a website powered by a Node-express server

As a newcomer to this project, I currently have an index.html, record.html, an application.js file handling logics and XMLhttp responses, as well as a style.css file.

Here is my node-express server which is running locally for now, but will eventually need to be deployed on AWS. I'm wondering about the best way to structure this project. Is it acceptable to place all the HTML, JS, and CSS files in a 'public' folder while keeping the node server files separate? Also, I am not writing any JavaScript in the node server - is this considered good practice? Thank you in advance for your help!

Server:

app.use(express.static(__dirname+'/public'));
app.get('/record', function(req, res) {
       res.sendFile(path.join(__dirname + '/public'+ '/record.html'));
});


app.get('/', function (req, res) {
       fs.readFile('/index.html', function(error, content) {
          if (error) {
          res.writeHead(500);
          res.end();
       } else {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(content, 'utf-8');
    }
}); 
  res.send('Hello World');   
});

https.createServer({
   key: privateKey,
   cert: certificate
}, app).listen(8080);

httpServer.listen(8443);

Answer №1

It appears that setting up an HTTP server is all you need.

Your static content will be delivered from the public folder, following best practices. If there's an index.html file in your public directory, it should automatically load when you go to http://localhost:8080

// Necessary packages are imported
var express = require('express');

// Creating our Express app instance
var app = express();

// Middleware for serving static files
app.use(express.static(__dirname + '/public'));

// Setting up an Express router
var router = express.Router();

// Initial test route
router.get('/', function(req, res) {

});

// Adding routes to the app
app.use(router);

// Starting the server
app.listen(8080);

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

Appear gradually and descend

Is there a way to enhance the animation on my homepage headline? Currently, I have set it to fade in when the page loads using CSS. I came across some sites with impressive animations that not only fade in the title but also move it down slightly. How can ...

Prevent the onclick function of a span element from being triggered when the user clicks on a dropdown menu contained within

Just delving into the world of web development and currently tackling a React project. Encountered an issue where I need to make a span element clickable. However, there's a dropdown within that span that doesn't have any onClick event attached t ...

Automatically sort with Angular and PHP

Currently, I am in the process of getting a search function to work smoothly. It involves loading data from a database using PHP to Angular, and everything seems to be displaying correctly prefilled, with one exception. There is a select option where the ...

Creating an Elastic Beanstalk instance from an s3 bucket using the aws-sdk tutorial

I am currently facing some difficulties in deploying an elastic beanstalk instance using the AWS JavaScript SDK. My goal is to have the source code come from an S3 bucket. While I know this can be done through the web interface, I am struggling to figure o ...

What could be causing my links to not appear in Internet Explorer 9?

Why are my links not showing up properly in Internet Explorer 9? If you prefer not to visit the website, you can directly access the style sheet at... This issue is specific to IE 9 and does not occur in other versions of Internet Explorer. ...

Achieving precise column alignment in Bootstrap 4

After searching extensively on Google and various websites, I am still struggling to find a solution for vertically aligning columns to be centered. Many sources I found mention aligning horizontally, but none seem to address the issue of vertical alignmen ...

Excessive Width Issue Caused by Bootstrap 4 Navbar Items

Having trouble with my Bootstrap 4 Navbar menu implementation. When I add more items, they overflow the container instead of floating correctly. Any CSS suggestions to temporarily fix this issue? <!DOCTYPE html> <html lang="en"> <head> ...

Exploring the functionality of Leader Line in VueJS

Attempting to implement Leader Line on VueJS has presented some challenges that have yet to be fully resolved online. The installation of leader-line using npm was successful - npm install leader-line Below is the code for the vuejs file: HTML: <div ...

Extracting data exclusively from the Node.js MySQL result object

How can I retrieve a list of unique values from a MySQL Column and format them into a single JSON array? This is the current code snippet I have: app.get('/experiences/', function(req, res) { res.setHeader('Access-Control-Allow-Origin& ...

CombineReducers takes all the reducer content and unpacks it into a single reducer

As I work on developing a small application, I am contemplating the best strategy for organizing reducers within it. My objective is to retrieve JSON data from the application state and structure it as shown below: { "fruits": [ {"appl ...

Unable to add npm package at this time

Feeling a bit desperate right now. I recently implemented npm and grunt to enhance my development process, which was working smoothly until today. Suddenly, I'm unable to install npm packages and keep encountering the following error message: 0 info ...

What is the best way to store an HTML header text file in a PHP variable?

Here is a snippet of my HTML file: <form action="cnvrt.php" method="POST" > Enter your text here - <input type="text" id="in" name="in"> <br> <input type="submit" value="submit" > </form> This is how my PHP file look ...

Having trouble viewing the value of request headers in Firebase?

Here is my code snippet: var headers = new Headers(); headers.append("bunny", "test"); headers.append("rabbit", "jump"); fetch("blahurl.com/someservice", {headers:headers}) When it comes to handling the request on Firebase: export const listener = funct ...

Is it possible to create a numerical unique identifier? What is the best way to resolve the issue of duplicate IDs safely?

I have a question regarding an old code that manually generates a random ID as a number, which has recently led to a duplicate ID issue. What is the most secure and effective solution to address this problem? I am considering using UUID to generate an ID, ...

What is the best way to retrieve the id of the chosen option within a <select> element?

In my index.html file, I have a < select > field and a button: <select id="mylist"></select> <input type="button" id="btn" value="update"> The provided Javascript code is designed to update the options of the < select > fie ...

Mouse over condensed text to show more information without impacting nearby elements

I have implemented a text-overflow: ellipsis on an element, and then added a hover effect to expand the text. How can I prevent this expanded text from affecting other elements above or below it? Here's how I've set the text: .box h3 { overfl ...

Tips for accessing the 'index' variable in *ngFor directive and making modifications (restriction on deleting only one item at a time from a list)

Here is the code snippet I'm working with: HTML: <ion-item-sliding *ngFor="let object of objectList; let idx = index"> <ion-item> <ion-input type="text" text-left [(ngModel)]="objectList[idx].name" placeholder="Nam ...

Determining whether a chosen element possesses a specific attribute

I have appended two different types of lists to my select element. One list contains users: <option value="domain\davidr" userid="108">David</option> The other list contains groups: <option value="Test Group" groupid="10">Test Grou ...

Modifying hidden field values with JavaScript does not carry over to the server side in Chrome

I created a custom user control that contains a hidden field which is set when a node is clicked on a Tree View Hierarchy control. The following function is responsible for handling the click event of the Tree View: function HandleTreeClick(evt) { va ...

regex: sequences containing a dot in the center

Struggling with a regex challenge - need to validate two words with a mandatory point between them. No special characters, no @ symbol, and no spaces allowed. The format should be like this: test.test I've been attempting to use the following regex ...