-
Download Node.js:
- Go to the Node.js download page.
- Download the LTS version as it is more stable and recommended for most users.
-
Install Node.js:
- Run the downloaded installer.
- Follow the prompts and ensure the "Add to PATH" option is selected.
- Complete the installation.
-
Verify Installation:
- Open Command Prompt.
- Run the following commands to check versions:
node -v npm -v
- You should see version numbers for both Node.js and NPM.
-
Download MySQL:
- Visit the MySQL Community Downloads page.
- Download and install the MySQL Community Server for Windows.
-
Install MySQL:
- Follow the installation instructions, and set up a root password for the database.
- Choose the default port 3306.
- Ensure MySQL is set to run as a Windows Service.
-
Start MySQL Server:
- Open the MySQL Workbench or run the command prompt as an administrator, and enter the following to start MySQL:
mysql -u root -p
- Enter the password you set during installation to access the MySQL CLI.
- Open the MySQL Workbench or run the command prompt as an administrator, and enter the following to start MySQL:
-
Create a Database for Your Application:
- In the MySQL CLI, create a new database:
CREATE DATABASE mean_stack;
- This database will be used by your Express app.
- In the MySQL CLI, create a new database:
-
Install Angular CLI via NPM:
- Run the following command in Command Prompt:
npm install -g @angular/cli
- Run the following command in Command Prompt:
-
Verify Installation:
- Check the Angular CLI version by running:
ng version
- You should see the version details if it installed correctly.
- Check the Angular CLI version by running:
-
Generate an Angular Project:
- In Command Prompt, navigate to the directory where you want your project, and run:
ng new my-mean-app
- Angular CLI will prompt you to choose styles (CSS, SCSS, etc.)—select according to your preference.
- In Command Prompt, navigate to the directory where you want your project, and run:
-
Run the Angular Application:
- Navigate to the project directory:
cd my-mean-app - Start the Angular development server:
ng serve
- Open a browser and go to
http://localhost:4200to see your running Angular app.
- Navigate to the project directory:
-
Navigate to the Backend Directory:
- Within your Angular project, create a folder for your backend server:
mkdir backend cd backend
- Within your Angular project, create a folder for your backend server:
-
Initialize a New Node Project:
- Run:
npm init -y
- This will create a
package.jsonfile.
- Run:
-
Install Express and MySQL2:
- Install the necessary packages:
npm install express mysql2
- Express will handle server requests, and mysql2 will help connect to the MySQL database.
- Install the necessary packages:
-
Create a Simple Server File:
- In the
backenddirectory, create anapp.jsfile with the following code:const express = require('express'); const mysql = require('mysql2'); const app = express(); // Middleware app.use(express.json()); // MySQL Database Connection const db = mysql.createConnection({ host: 'localhost', user: 'root', password: 'your_mysql_password', // Replace with your MySQL root password database: 'mean_stack' }); db.connect(err => { if (err) { console.error('MySQL connection error:', err); return; } console.log('Connected to MySQL'); }); // Routes app.get('/', (req, res) => { res.send('Hello from Express and MySQL!'); }); // Start Server const PORT = 3000; app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });
- Note: Replace
'your_mysql_password'with the password you set for MySQL.
- In the
-
Start the Server:
- In Command Prompt, run the following in the
backendfolder:node app.js
- You should see a message indicating that the server is connected to MySQL.
- In Command Prompt, run the following in the
-
Proxy Configuration:
- In the Angular app (
my-mean-app), create aproxy.conf.jsonfile with the following:{ "/api": { "target": "http://localhost:3000", "secure": false } } - This will forward requests from Angular to your Express server.
- In the Angular app (
-
Update Angular
angular.json:- Add this line in the
"serve"section:"proxyConfig": "src/proxy.conf.json"
- Add this line in the
-
Restart the Angular Development Server:
- Run:
ng serve
- Angular Requests to
/apiwill now be proxied to the Express server.
- Run: