Server side init flow

Step 1

$ mkdir server
$ touch server/index.js
$ npm init -y
$ npm install express --save
$ node server/index.js
$ npm install --save-dev babel-cli babel-preset-env

index.js

import express from 'express';
let app = express();
app.get('/', (req, res) => { res.send('hello world'); })
app.listen(6060, () => console.log('Running on localhost:6060'));

Step 2

$ touch .babelrc

.babelrc

{
  "presets": ["env"]
}

package.json

"scripts": {
    "start": "nodemon --watch server --exec babel-node -- server/index.js"
  }

Step 3

$ mkdir server/routes
$ touch server/routes/users.js

users.js

import express from 'express'

let router = express.Router()

router.get('/',(req,res)=>{
  console.log('get success')
})

router.post('/',(req,res)=>{
  console.log('post success')
})

export default router

index.js

import express from 'express';
import users from './routes/users'

let app = express();

app.use('/api/users', users)

app.get('/', (req, res) => {
  res.send('hello world');
})

app.listen(6060, () => console.log('Running on localhost:6060'));

Last updated