Node js server setup using Express js framework, which serves static files from a directory named 'p

Опубликовано: 25 Июль 2026
на канале: if programming and coding 兰 aegis ༺
20
0

i enter http://localhost:5500/ now, here is my code: // server.js // start server and connect http://localhost:5500/
const express = require('express');
const mysql = require('mysql');

const app = express();

app.use(express.static('public'));

// Establish MySQL connection
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'hospital'
});

// Connect to MySQL
connection.connect((err) = {
if (err) {
console.error('Error connecting to MySQL: ' + err.stack);
return;
}
console.log('Connected to MySQL as id ' + connection.threadId);
});


// Start the server
const PORT = 5500;
app.listen(PORT, () = {
console.log(Server is running on http://localhost:${PORT});
});
===================================

This code is a Node.js server setup using Express.js framework, which serves static files from a directory named 'public'. It also establishes a connection to a MySQL database.

Here's a breakdown of what each part does:

Express Setup:
const express = require('express');: Imports the Express.js framework.
const app = express();: Creates an instance of the Express application.

Serve Static Files:
app.use(express.static('public'));: This line tells Express to serve static files from the 'public' directory. Static files include HTML, CSS, JavaScript, images, etc. This allows you to serve your frontend files like HTML, CSS, and client-side JavaScript.

MySQL Connection:
const mysql = require('mysql');: Imports the MySQL module.
const connection = mysql.createConnection({ ... });: Creates a connection to a MySQL database. You specify the host, username, password, and database name in the configuration object.
connection.connect((err) = { ... });: Attempts to connect to the MySQL database. If an error occurs during the connection process, it logs the error message. Otherwise, it logs a success message with the connection thread ID.

Start Server:
const PORT = 5500;: Specifies the port number on which the server will listen for incoming requests.
app.listen(PORT, () = { ... });: Starts the Express server and listens for incoming requests on the specified port. Once the server is started, it logs a message indicating that the server is running and the URL at which it is accessible.

To use this code:

Make sure you have Node.js and npm (Node Package Manager) installed on your system.
Create a directory for your project and navigate into it using the terminal.
Initialize a new Node.js project by running npm init and follow the prompts to create a package.json file.
Install Express and MySQL modules by running npm install express mysql in your project directory.
Create a 'public' directory and put your static files (HTML, CSS, JavaScript, etc.) inside it.
Create a file named 'server.js' and paste the provided code into it.
Replace 'localhost', 'root', 'password', and 'hospital' with your actual MySQL database credentials and database name.
Run your server by executing node server.js in the terminal.
Access your server in the browser by navigating to http://localhost:5500/.

This setup will serve your static files from the 'public' directory and establish a connection to your MySQL database. The server will listen for incoming HTTP requests on port 5500.