FP4: Express Server

Charlie·2021년 1월 1일
0

First Project

목록 보기
4/8
post-thumbnail
  • Express
  • Cors
  • Cookie-parser
// 'app.js' from 'root' Directory 
const express = require('express');
const cookieParser = require('cookie-parser');
const cors = require('cors');
require('dotenv').config();

const app = express();
// const path = require('path');
// app.use(express.static(path.join(__dirname, '/public')));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(cors({
  origin: [ 'http://localhost:3000'],
  methods: ['OPTIONS','GET','POST','PUT','DELETE'],
  credentials: true
}));

const usersRouter = require('./routes/users');
const postsRouter = require('./routes/posts');
const commentsRouter = require('./routes/comments');
app.use('/users', usersRouter);
app.use('/posts', postsRouter);
app.use('/comments', commentsRouter);

app.get('/', (req, res) => {
  // res.status(200).send('Hello world');
  res.redirect('/posts/read');
});

const port = process.env.baseUrl_port;
const baseUrl = process.env.baseUrl || localhost;
app.listen(port, () => {
  console.log(`App is running at http://${baseUrl}:${port}`);
});

0개의 댓글