has power
What is it?
Open source JavaScript runtime built on Chrome's V8 Javascript engine.
The big players
Yet another dependency?
Fast & minimalist web framework for Node.js. No need to reinvent the wheel on yours own!
each language has its own Node.js
* or MongoDB Atlas
* or redislabs
$ node -v
v10.1.0
// macOS
$ brew install node
// Linux
$ sudo apt install -y nodejs
$ yarn -v
1.7.0
// macOS
$ brew install yarn
// Linux
$ sudo apt install -y yarn
$ mongo --version
MongoDB shell version v4.0.3
// macOS
$ brew install mongo
// Linux
$ sudo apt install -y mongodb-org
$ redis-server --version
Redis server v=4.0.9
// macOS
$ brew install redis
// Linux
$ sudo apt install -y redis-server
$ mkdir has_power
$ cd has_power
$ yarn init
$ yarn add express
$ touch index.js
Init the files
const express = require('express')
const app = express();
const port = 3000;
app.get(
'/',
(req, res) => res.send('Has power!'),
);
app.listen(
port,
() => console.log(`localhost:${port}`),
);
index.js
Workshop starter (ES6)
https://github.com/vintage/
jshp-node-starter.git
Development time
Hello World Czatex
Endpoints
import express from 'express'
const router = express.Router()
router.get('/messages', getMessages)
router.post('/messages', createMessages)
const app = express()
app.use(router)
Through router
// db.js
import { MongoClient } from 'mongodb'
const client = new MongoClient(
'conn_string',
{ useNewUrlParser: true },
)
// server.js
client.connect(() => {
// Mongo is ready
// Start the express server
})
MongoDB
const db = client.db('app')
const collection = db.collection('messages')
collection.find(filters).toArray()
collection.findOne(filters)
collection.insertOne(record)
collection.updateOne(
filters, { $set: newData },
)
collection.deleteOne(filters)
MongoDB is async
Centralized database
mongodb+srv://<user>:<password>@jshp-rxodt.mongodb.net/app?retryWrites=true
import express from 'express'
const app = express()
app.use('/static', express.static('public'))
Express built-in
import fileUpload from 'express-fileupload'
const app = express()
app.use(fileUpload())
const filePath = '/tmp/file.png'
fs.writeFile(
filePath,
'some data',
function() {
console.log('File saved')
},
)
Express add-on
socket.io
Sockets with simple frontend