Node.js
has power
Node.js
What is it?
Open source JavaScript runtime built on Chrome's V8 Javascript engine.
Who's using it?
The big players
Express.js
Yet another dependency?
Fast & minimalist web framework for Node.js. No need to reinvent the wheel on yours own!
Alternative tech
each language has its own Node.js
Prerequisites
- JavaScript (ES6)
- Basic experience with command line tools
- Being familiar with dependency management tool - yarn or npm
- Client-server model
Environment
- node>=10, yarn>=1.7
- mongo>=4.0*
- redis-server>=4.0*
* or MongoDB Atlas
* or redislabs
node.js
$ node -v
v10.1.0
// macOS
$ brew install node
// Linux
$ sudo apt install -y nodejs
yarn
$ yarn -v
1.7.0
// macOS
$ brew install yarn
// Linux
$ sudo apt install -y yarn
mongo
$ mongo --version
MongoDB shell version v4.0.3
// macOS
$ brew install mongo
// Linux
$ sudo apt install -y mongodb-org
redis
$ 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
Create project
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}`),
);
Create project
index.js
Create project
Workshop starter (ES6)
https://github.com/vintage/
jshp-node-starter.git
Let's work
Development time
Workshop app
Hello World Czatex
Czatex
Endpoints
- GET /random-name
- POST /auth/signup
- POST /auth/signin
- POST /auth/signout
- GET /users
- GET /messages
- POST /messages
- GET /messages/<MSG_ID>
- DELETE /messages/<MSG_ID>
- POST /messages/<MSG_ID>/like
- DELETE /messages/<MSG_ID>/like
import express from 'express'
const router = express.Router()
router.get('/messages', getMessages)
router.post('/messages', createMessages)
const app = express()
app.use(router)
Routing
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
})
Data persistence
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)
Data manipulation
MongoDB is async
Czatex
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'))
Static files
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')
},
)
Media files
Express add-on
Czatex
socket.io
Sockets with simple frontend
Node.js has power
By Kamil Rykowski
Node.js has power
- 249