0% found this document useful (0 votes)
8 views

Project Setup Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Project Setup Guide

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Web Programming Project Setup Guide

Step 1: Initialize the Project

Create the project folder:


```bash
mkdir project && cd project
```

Initialize Node.js:
```bash
npm init -y
```

Install the dependencies:


```bash
npm install cookie-parser dotenv ejs express express-handlebars multer mysql2
```

Step 2: Configure the Environment

Create a `.env` file to store your environment variables:

```env
DB_HOST=localhost
DB_USER=wpr
DB_PASS=fit2024
DB_NAME=wpr<student_id>
PORT=8000
```

Step 3: Create the Database Setup Script (dbsetup.js)

This script sets up the MySQL database and initializes it with the required data:

```js
const mysql = require('mysql2');
require('dotenv').config();

const connection = mysql.createConnection({


host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
multipleStatements: true
});

const dbName = process.env.DB_NAME;

const setupDatabase = \`
CREATE DATABASE IF NOT EXISTS ${dbName};
USE ${dbName};
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(255),
email VARCHAR(255) UNIQUE,
password VARCHAR(255)
);
CREATE TABLE IF NOT EXISTS emails (
id INT AUTO_INCREMENT PRIMARY KEY,
sender_id INT,
recipient_id INT,
subject VARCHAR(255),
body TEXT,
attachment VARCHAR(255),
sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (sender_id) REFERENCES users(id),
FOREIGN KEY (recipient_id) REFERENCES users(id)
);
INSERT INTO users (full_name, email, password) VALUES
('User One', '[email protected]', '123'),
('User Two', '[email protected]', '123456'),
('User Three', '[email protected]', 'password');
INSERT INTO emails (sender_id, recipient_id, subject, body) VALUES
(1, 2, 'Hello', 'This is an email body'),
(2, 1, 'Hi there', 'This is another email body');
\`;

connection.query(setupDatabase, (err) => {


if (err) throw err;
console.log('Database setup complete');
connection.end();
});
```

Run the script using:


```bash
node dbsetup.js
```

Step 4: Application Entry Point (app.js)

```js
const express = require('express');
const cookieParser = require('cookie-parser');
const dotenv = require('dotenv');
const path = require('path');
const authRoutes = require('./routes/auth');
const emailRoutes = require('./routes/email');

dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));

// Routes
app.use('/', authRoutes);
app.use('/emails', emailRoutes);

const PORT = process.env.PORT || 8000;


app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});
```
Step 5: Routes (routes/auth.js and routes/email.js)

1. **`routes/auth.js`** (Handles sign-in and sign-up):


```js
const express = require('express');
const router = express.Router();

// Add routes for sign-in and sign-up

module.exports = router;
```

2. **`routes/email.js`** (Handles emails):


```js
const express = require('express');
const router = express.Router();

// Add routes for inbox, outbox, and email detail

module.exports = router;
```

Step 6: Views (EJS templates)

Create EJS templates under the `views` folder for `signin`, `signup`, `inbox`, etc., to render
HTML forms and pages based on user actions.

You might also like