Eric Whale

Build file structure for server

node_modules/
.DS_Store
.env
......
const jwt = require("jsonwebtoken");
const bcrypt = require("bcryptjs");
const asyncHandler = require("express-async-handler");
// @desc Signup new user
// @route POST /api/users
// @access Public
const signupUser = asyncHandler(async (req, res) => {
const { username, email, password } = req.body;
if (!name || !email || !password) {
res.status(400);
throw new Error("Please fill in all fields");
}
// Check if user already exists
// Hash password (bcrypt)
// Create/Build user
// Send response
});
const loginUser = asyncHandler(async (req, res) => {
const { email, password } = req.body;
// Check for the user email
// Send response
});
module.exports = {
signupUser,
loginUser,
};
......@@ -11,6 +11,7 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-router-dom": "^6.3.0",
......@@ -4523,6 +4524,28 @@
"node": ">=12"
}
},
"node_modules/axios": {
"version": "0.27.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
"dependencies": {
"follow-redirects": "^1.14.9",
"form-data": "^4.0.0"
}
},
"node_modules/axios/node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/axobject-query": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
......@@ -19468,6 +19491,27 @@
"resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.4.2.tgz",
"integrity": "sha512-LVAaGp/wkkgYJcjmHsoKx4juT1aQvJyPcW09MLCjVTh3V2cc6PnyempiLMNH5iMdfIX/zdbjUx2KDjMLCTdPeA=="
},
"axios": {
"version": "0.27.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz",
"integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==",
"requires": {
"follow-redirects": "^1.14.9",
"form-data": "^4.0.0"
},
"dependencies": {
"form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
}
}
},
"axobject-query": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
......
......@@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.27.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-router-dom": "^6.3.0",
......@@ -20,17 +21,10 @@
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
"extends": ["react-app", "react-app/jest"]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"production": [">0.2%", "not dead", "not op_mini all"],
"development": [
"last 1 chrome version",
"last 1 firefox version",
......
const axios = require("axios").default;
export const handleLogin = (email, pwd) => {
console.log(email, pwd);
axios
.post("http://localhost:8000/api/login", {
email,
pwd,
})
.then(function (response) {
console.log("login request sent");
console.log(response);
})
.catch(function (err) {
console.log(err);
});
};
import { useState } from "react";
// lib
import { handleLogin } from "../lib/auth";
// styles
import { Link } from "react-router-dom";
import "../styles/layout.scss";
function Login() {
const [email, setEmail] = useState("");
const [pwd, setPwd] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log("login form submit called");
handleLogin(email, pwd);
};
return (
......@@ -20,10 +26,22 @@ function Login() {
<form className="authForm" onSubmit={(e) => handleSubmit(e)}>
<label htmlFor="email">
email: <input type="text" id="email" />
email:{" "}
<input
onChange={(e) => setEmail(e.target.value)}
value={email}
type="text"
id="email"
/>
</label>
<label htmlFor="password">
password: <input type="text" id="password" />
password:{" "}
<input
onChange={(e) => setPwd(e.target.value)}
value={pwd}
type="text"
id="password"
/>
</label>
<label htmlFor="submit">
<input type="submit" id="submit" />
......
const colors = require("colors");
const mongoose = require("mongoose");
const connectDB = async () => {};
module.exports = connectDB;
const errorHandler = (err, req, res, next) => {
const statusCode = res.statusCode ? res.statusCode : 500;
res.status(statusCode);
res.json({
message: err.message,
// stack from mongoDB (maybe...)
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
};
module.exports = {
errorHandler,
};
{
"name": "weather_chatbot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"description": "web-app weather chatbot",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "node server.js",
"dev": "nodemon server.js"
},
"repository": {
"type": "git",
......@@ -13,7 +14,12 @@
"author": "황선혁",
"license": "MIT",
"dependencies": {
"express": "^4.18.1"
"bcryptjs": "^2.4.3",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-async-handler": "^1.2.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.3.4"
},
"devDependencies": {
"nodemon": "^2.0.16"
......
const express = require("express");
const router = express.Router();
const { signupUser, loginUser } = require("../actions/userActions");
router.post("/", signupUser);
router.post("/login", loginUser);
module.exports = router;
const express = require("express");
const dotenv = require("dotenv").config();
const { errorHandler } = require("./middleware/errorMiddleware");
const connectDB = require("./config/db");
const port = process.env.PORT || 8000;
connectDB();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api/users", require("./routes/userRoutes"));
app.use(errorHandler);
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
const express = require("express");
const app = express();
const PORT = 8000;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.get("/login", (req, res) => {
res.send("Hello World!");
});
app.get("/signup", (req, res) => {
res.send("Hello World!");
});