You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Every request has method, status, headers, cookies and body.
Methods
GET
POST
PUT
DELETE
OPTION, etc
Headers - metadata
Body - info that we're sending to backend (POST, PUT)
Cookies - information that backend sends to keep in browser to use it in the future as part of header for validation. On the browser side there's an api that sends cookie automaticaly to backend so you don't need to do it manually from frontend. It's safer than using localstorage but in mobile it's a headache (as everything else in mobile).
Express
node index.js --inspect - allows us to debug node in browser
Request (from HTTP2) and Response are streams and if you're using express there's a way to get response as chunk.
REST API
GET, POST, PUT, DELETE
How to create urls
/posts/:postId/comments/:commentId
Query params - we can get them from req.query.
/posts?last-20-10-2019&index=10
Middleware
Before response came to handler there're a lot of function that can affect on it. These functions are middlewares.
There's a way to create middleware - app.use and it has 3rd argument - next. If we call it without args, then it goes to another middlewares. In case if we pass object - it causes the error which we can handle by app.on('error', cb).
We can send headers by calling res.header('X_COMP_MAN', 'MAC').
Example
app.use([route],(req,res,next)=>{console.log('THIS IS MIDDLEWARE');next();});
Example's of REST queries
GET /users
app.get('/users',(req,res)=>{res.json(users);})
GET /users/:id
POST /users
We also need middleware for parsing body.
Cookies has key, value, ExpirationDate, secure, HTTPOnly, domain, path, etc
How to set cookies? Let's do it on user creation.
constusers=[];app.post('/users',(req,res)=>{constnewUser=req.body;users.push(newUser);res.cookie('uid',newUser.id,{httpOnly: true,secure: false,expires: newDate('10-10-2099')});res.json(newUser);res.end();});app.get('/users',(req,res)=>{res.cookie('uid',1,{httpOnly: true,secure: false,expires: newDate('10-10-2099')}).end();});// better to do this process in middlewareapp.get('/me',(req,res)=>{const{uid}=req.cookies;constuser=users.find(u=>u.id===+uid)if(!user){res.status(404).end()return}res.json(user);});
If you want to delete cookie, you need to change it's expires key to past date.
We can use this feature of express to create a lot of mini independent reusable applications and connect them to route (e.g. user handler to user, posts handler to post).
Other express handler arguments
Request
Response
Express methods
Download - to get file and stream response by sending using chunks.
Task,
to make download process on server slower (CPU safe way)
Another Packages
We use dotenv to insert variables to process.env.
We can use winston as logger. It has some advantages over morgan. For example, morgan can log only requests, winston on the other side can work with everything. Also, winston has tons of integrations (e.g. with mongodb)
Protocol - set of communication rules for two entities communication (e.g. client / server).
Networking
DNS
Protocols
Every protocol has a multiple layers.
HTTP(S) - Hyper Text Transfer Protocol - Application Layer Protocol
We've client and server and they setup communication using handshake request
HTTP uses TCP/IP
Before HTTP/1.0 every request (e.g styles, scripts) had it's own handshake request. After HTTP/1.0 we can setup "keep-alive" method to keep request alive.
HTTP/2.0 forces to use HTTPS and can get all files that it might need in future.
HTTP/3.0 gonna use UDP
HTTPS only have SSL, SSL makes sure domain that opened belongs to ip
TCP / IP - Transport Layer Protocols
It's a socket that opens so client-server can communicate
TCP/IP says that data will be there and in the correct order
UDP - User Datagram Protocol - Transport Layer Protocols
RTC - Realtime Communication
SSH - Secure Shell
FTP - File Transfer Protocol
DRP - Remote Desktop Protocol
TCP/IP
We've net package which allows to create tcp server
server.js
constnet=require('net');constserver=net.createServer();server.on('listening',()=>{console.log('tcp server listening');});server.on('connection',socket=>{console.log(socket.localAddress);// data is a Bufferserver.on('data',(data)=>{console.log(data.toString());socket.write('ping');});// destroys socketsocket.destroy();});server.listen(1234);