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

[최지준] Assignment 2: Express API - 유저 회원가입 하기 #10

Open
wants to merge 1 commit 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
16 changes: 9 additions & 7 deletions jijun/.env.sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
DB_CONNECTION = mysql
DB_HOST = 127.0.0.1
DB_USERNAME = root
DB_PASSWORD = myPassword
DB_DATABASE = myDB
DB_PORT = 3306
DB_LOGGING =TRUE
DATABASE_URL="mysql://username:[email protected]:3306/myDB"

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_USERNAME=root
DB_PASSWORD=myPassword
DB_DATABASE=myDB
DB_PORT=3306
DB_LOGGING=TRUE
23 changes: 17 additions & 6 deletions jijun/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const { DataSource } = require('typeorm');
const bcrypt = require('bcrypt');
// const saltRounds = 12;

const PORT = 3000;
const app = express();

const appDataSource = new DataSource({
Expand All @@ -16,19 +19,27 @@ const appDataSource = new DataSource({
database: process.env.DB_DATABASE,
});

appDataSource.initialize().then(() => {
console.log('Data Source has been initialize!');
});

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(morgan('dev'));

app.get('/ping', (req, res) => {
res.json({ message: 'pong' });
});

app.listen(3000, async () => {
app.post('/users/signup', async (req, res, next) => {
const { name, email, password } = req.body;
const hashPwd = bcrypt.hashSync(password, 12);

await appDataSource.query(
`INSERT INTO users (name, email, password) VALUES (?,?,?);`,
[name, email, hashPwd]
);
res.status(201).json({ message: 'userCreated' });
});

app.listen(PORT, async () => {
await appDataSource
.initialize()
.then(() => {
Expand All @@ -37,5 +48,5 @@ app.listen(3000, async () => {
.catch((error) => {
console.error('Error during Data Source initialize', error);
});
console.log(`Listening to request on port: 3000`);
console.log(`Listening to request on port: ${PORT}`);
});
Loading