Posts

Showing posts with the label node js

nodejs socketio

Image
  Socketio is a library based on websocket. It supports stateful protocol. Cdn: <script src=" https://cdn.socket.io/4.7.4/socket.io.min.js "></script> Note: I have inserted socket.js inside the ‘public’ folder. index.js const express = require('express') const app = express() const port = 3000 const path = require('path') // socketio integration const { createServer } = require('node:http'); const server = createServer(app); const { Server } = require('socket.io'); const io = new Server(server); app.use('/public', express.static('public')) app.get('/',function(req, res){     res.sendFile(path.resolve(__dirname, 'templates/message.html')) }) io.on('connection', (socket) => {     console.log('a user connected');     socket.on('disconnect', () => {       console.log('user disconnected');     });     socket.on('chat_message', (msg) => { ...

expressjs cheat sheet

Expressjs is a npm package or we can say a web framework for nodejs. When we working on a node js-express js project we need several packages. “npm install express” -> it will install the express js module inside the node project.  Let’s make a simple express js app. const express = require('express') const app = express() const path = require('path') const port = 3000 // basic get request for default route app.get('/',function(req, res){     res.send('welcome to home i am shimanta das') }) // send HTML file as response on '/about' route app.get('/about',function(req, res){     res.sendFile(path.join(__dirname, 'about.html')) }) app.listen(port,function(){     console.log('app listen port', port); }) When you are working on this app, every time when you make some changes into this app, you should also need to restart the server every time, for preventing this problem, you can use ‘nodemon’. It's a kind of reloader f...