What I learnt (Part 1) Introduction and Express Server

What I learnt (Part 1) Introduction and Express Server

Written by Victor Nikliaiev | Published 19 days ago

Introduction


Hello, there! Thanks for the opportunity to place this stuff right here.
I'll write this and following articles by a special way according to one format or schema to get it easier to catch and navigate in case if I or someone would like to .

But for first at all, I'd like to introduce myself. My name's Victor, I am from Ukraine. I'm some kind of not a newbie at the coding, but in matter of fact I'm not a developer yet. Need to learn much more stuff to become one. What I've took this course for? Well... I want to have my own blog. Where I'd share some writings(I'm interested in it as well) with an audience. This is like to hook two birds with a shot. The blog for writing, and writing for a blog. :D Hope you guys got what I meant. Let's get started.


Part 1


What I learnt in this lection:


  1. How to setup an express server in an easy way and do some fancy stuff with some additional node modules.
  2. Setup an environment for some variables.
  3. Connect to local Mongo Database.


Realization:

npm i express mongoose body-parser cookie-parser morgan nodemon dotenv cors


.env
NODE_ENV=development
PORT=8000
CLIENT_URL=http://localhost:3000
DATABASE_LOCAL='mongodb://localhost:27017/seoblog'



server.js
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
require('dotenv').config() 

// app
const app = express()

// db
mongoose.connect(process.env.DATABASE_LOCAL, {
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false,
  useUnifiedTopology: true
}).then(()=> console.log('DB is connected'));

// cors
if(process.env.NODE_ENV === 'development') {
  app.use(cors({origin: `${process.env.CLIENT_URL}`}))
}

// port 
const port = process.env.PORT || 8000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`)
})


RELATED BLOGS