Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test hi #12

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions back/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock

.env
11 changes: 11 additions & 0 deletions back/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"watch": [
"src",
".env"
],
"ext": "js,ts,json",
"ignore": [
"src/**/*.spec.ts"
],
"exec": "ts-node --transpile-only ./src/index.ts"
}
31 changes: 31 additions & 0 deletions back/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "back",
"version": "1.0.0",
"main": "src/index.js",
"scripts": {
"dev": "ts-node src",
"build": "tsc && node dist",
"test": "nodemon"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.10.0",
"gravatar": "^1.8.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.3"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/express": "^4.17.11",
"@types/mongoose": "^5.10.4",
"@types/node": "^14.14.37",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
},
"author": "",
"license": "ISC",
"description": ""
}
19 changes: 19 additions & 0 deletions back/src/Logger/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from "mongoose";
import config from "../config";

const connectDB = async () => {
try {
await mongoose.connect(config.mongoURI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
});

console.log("Mongoose Connected ...");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};

export default connectDB;
Empty file added back/src/api/profile.ts
Empty file.
87 changes: 87 additions & 0 deletions back/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import express from "express";
import gravatar from "gravatar";
import jwt from "jsonwebtoken";
import bcrypt from "bcryptjs";
import config from "../config";
import { check, validationResult } from "express-validator";

const router = express.Router();

import User from "../models/User";

/**
* @route Post api/users
* @desc Register User
* @access Public
*/
router.post(
"/",
[
check("name", "Name is required").not().isEmpty(),
check("email", "Please include a valid email").isEmail(),
check(
"password",
"Please enter a password with 6 or more characters"
).isLength({ min: 6 }),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

const { name, email, password } = req.body;

try {
// See if user exists
let user = await User.findOne({ email });

if (user) {
res.status(400).json({
errors: [{ msg: "User already exists" }],
});
}

// Get users gravatar
const avatar = gravatar.url(email, {
s: "200",
r: "pq",
d: "mm",
});

user = new User({
name,
email,
avatar,
password,
});

// Encrpyt password
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(password, salt);

await user.save();

// Return jsonwebtoken
const payload = {
user: {
id: user.id,
},
};
jwt.sign(
payload,
config.jwtSecret,
{ expiresIn: 36000 },
(err, token) => {
if (err) throw err;
res.json({ token });
}
);
} catch (err) {
console.error(err.message);
res.status(500).send("Server Error");
}
}
);

module.exports = router;
29 changes: 29 additions & 0 deletions back/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import dotenv from "dotenv";

// Set the NODE_ENV to 'development' by default
process.env.NODE_ENV = process.env.NODE_ENV || "development";

const envFound = dotenv.config();
if (envFound.error) {
// This error should crash whole process

throw new Error("⚠️ Couldn't find .env file ⚠️");
}

export default {
/**
* Your favorite port
*/
port: parseInt(process.env.PORT, 10),

/**
* That long string from mlab
*/
mongoURI: process.env.MONGODB_URI,

/**
* Your secret sauce
*/
jwtSecret: process.env.JWT_SECRET,
jwtAlgorithm: process.env.JWT_ALGO,
};
38 changes: 38 additions & 0 deletions back/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import express from "express";
const app = express();
import connectDB from "./Logger/db";

// Connect Database
connectDB();

app.use(express.json());

// Define Routes
app.use("/api/users", require("./api/users"));


// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "production" ? err : {};

// render the error page
res.status(err.status || 500);
res.render("error");
});

app
.listen(5000, () => {
console.log(`
################################################
🛡️ Server listening on port: 5000 🛡️
################################################
`);
})
.on("error", (err) => {
console.error(err);
process.exit(1);
});

//일단
14 changes: 14 additions & 0 deletions back/src/interfaces/IUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface IUser{
id: string;
name: string;
email: string;
password: string;
avatar: string;
date: Date;
}

export interface IUserInputDTO{
name: string;
email: string;
password: string;
}
31 changes: 31 additions & 0 deletions back/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose from "mongoose";
import { type } from "node:os";
import {IUser} from "../interfaces/IUser"

const UserSchema = new
mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
avatar: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
});

export default
mongoose.model<IUser &
mongoose.Document>("User",UserSchema);
21 changes: 21 additions & 0 deletions back/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2017",
"lib": ["es2017", "esnext.asynciterable"],
"typeRoots": ["./node_modules/@types", "./src/types"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"module": "commonjs",
"pretty": true,
"sourceMap": true,
"outDir": "./build",
"allowJs": true,
"noEmit": false,
"esModuleInterop": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules", "tests"]
}
24 changes: 24 additions & 0 deletions front/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock
8 changes: 8 additions & 0 deletions front/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"singleQuote": true,
"semi": true,
"useTabs": false,
"tabWidth": 2,
"trailingComma": "all",
"printWidth": 80
}
Loading