-
Open MySQL:
- If you use MySQL Workbench, open that. OR:
- If you have a Windows, open the MySQL Command Line Interface (CLI) tool. OR:
- If you have Mac, run
mysql -u root -pto open the MySQL CLI.
-
Create a database for your app by running
CREATE DATABASE your-app-name;(replace with your actual app name). Don't worry about tables yet, that will come later! Check that it worked:- If you're using MySQL Workbench, you should see your new database name in your database list on the left.
- If you're using the MySQL CLI (Mac or Windows), run
SHOW DATABASES;to see a list of your databases: the new one should be there.
-
On your computer, create a new (empty) folder that will hold your project. In your terminal, use
cdto navigate into your new project folder. -
From there, run the following to scaffold your Express server:
npx express-generator --no-view server
(If it prompts to install
express-generator, confirm by typing "y".) -
Follow the prompts in the terminal to
cd serverand install dependencies withnpm install. -
Install necessary packages:
mysql2,nodemon,dotenv, andcors:npm install mysql2 nodemon dotenv cors
-
Add the following lines to
app.jsto enable CORS:// Add this at the top: const cors = require("cors"); // Add this after 'app' is created: app.use(cors());
-
Comment out the following line in
app.js(around line 17) to prevent serving static files:// app.use(express.static(path.join(__dirname, 'public'))); -
Also in
app.js, update the path for your routers so they are prefixed with/api(around lines 18-19):app.use("/api", indexRouter); app.use("/api/users", usersRouter);
-
Copy the
datafolder from this repo into your project. This folder should contain:init_db.sql: A file containing the SQL code to build your database
-
Copy the
configfolder from this repo into your project. This folder should contain:db.js: A wrapper around DB connections, allowing the use ofpool.query()in your code.migrate.js: A migration file to (re)create DB tables and insert sample data.
-
Copy the
controllersfolder from this repo into your project. This is where you will build the logic for your routes. Currently, it just contains an example file so you can get everything connected. You can change and add to this later to fit your own needs. Look at your class activites if you'd like more examples of controller functions. -
As part of the automatic Express setup, you will have a
routesfolder with two starter files:index.jsandusers.js.Updateindex.jsto import and use your example controller. Your finalindex.jsfile should look like this:
const express = require("express");
const router = express.Router();
const getExample = require("../controllers/exampleController");
/* GET example */
router.get("/", getExample);
module.exports = router;-
Create a
.envfile (in the root project directory) to store your database connection information. You can follow the format in the.env.examplefile. It should include the name of your project's database, as well as your host, MySQL username and password. -
Modify the start script in
package.jsonto usenodemon:
"start": "nodemon ./bin/www"-
Add a new script in
package.jsonto run your migrations:"migrate": "node config/migrate.js"
Once your SQL starter code is finalized in your
init_db.sqlfile, runnpm run migrateto create your DB tables. If you need to modify your table later, you can update yourinit_db.sqlfile and runnpm run migrateagain. This will delete your old table(s) and recreate them with the new structure. -
Update the default port in
./bin/wwwfrom3000to4000(around line 15). -
Add a
.gitignorefile to your server with at least the following entries:node_modules/ package-lock.json .env .DS_Store -
Test your setup:
- Run
npm startinside yourserverfolder to start your back-end server. - Open Postman and run a GET request to http://localhost:4000/api
- You should see the successful response from your example controller:
{ message: "Welcome to Express" }
- Run
You're done! Happy back-end coding :)
-
In your terminal,
cd ..to go back to your main project folder. Scaffold your React front-end "client" using Vite:npm create vite@latest client -- --template react
Follow the prompts and choose React and Javascript.
-
Navigate into your client folder and install
axios:npm install axios
Configure the React front end so it communicates with the Express back end:
-
Open
vite.config.jsin your client, and configure a proxy to redirect API requests to the Express server:export default defineConfig({ plugins: [react()], server: { proxy: { "/api": { target: "http://localhost:4000", changeOrigin: true, secure: false, }, }, }, });
Note: All back-end routes should start with
/api. -
Test your setup:
-
cd serverandnpm startto run your server. -
cd clientandnpm run devto run your client. -
In your client's App.jsx:
- At the top of the file,
import axios from 'axios'. - Inside the App() function, paste the following test:
const testFetch = async () => { // because of our proxy in vite.config, we can now fetch directly to "/api" const response = await axios.get("/api"); console.log(response); }; testFetch();
- At the top of the file,
-
Check your browser console - you should see the successful response object there.
-
That's it! Happy front-end coding :)
Once you have created both your server and your client, it's time to create a Git repository, and push it onto GitHub:
-
In your terminal, make sure you are in your root project folder (i.e. not inside
serverorclient). -
Run
git initto initialize a local Git repository. -
Run
git add .andgit commit -m "initial commit"to stage and commit your initial setup. -
Open GitHub, select the
+sign in the top right corner, and select New repository. -
Create a new repository: choose your app name and click Create repository. Do NOT select the checkbod for "Add a README" file.
-
Follow the instructions provided by GitHub for "...or push an existing repository from the command line" by copying and pasting those commands into a terminal in each project folder on your computer.
-
Invite your instructor and TA to be collaborators:
- Go to the Settings tab of the repo on GitHub, then choose Collaborators in the left column, and press the Add People button. Grant admin access if necessary.
-
Happy coding!