In the last article, we cover the setup for the client-side of the application. Today we are going to look closely at server-side part. 🚀
Application API is written with Express framework for file serving and Websocket for communication. Entry file for server:
// ...dotenv.config();constport=process.env.PORT;constapp:Express=express();constserver=http.createServer(app);app.use(express.static(path.join(__dirname,'public')));app.get('(/*)?',async(req,res,next)=>{res.sendFile(path.join(__dirname,'public','index.html'));AppDataSource.initialize().then(async()=>{console.info('Database connected');}).catch((error)=>{console.error(error);constio=newServer<IncomingEvents,OutgoingEvents,{},User>(server,{transports:['websocket','polling'],io.on('connection',(socket:Socket<IncomingEvents,OutgoingEvents,{},User>)=>{registerUsersHandlers(io,socket);registerCardsHandlers(io,socket);registerBoardsHandlers(io,socket);server.listen(port,()=>{// eslint-disable-next-line no-consoleconsole.log(`Server is running at http://localhost:${port}`);
To communicate with the backend server is using TypeORM. Initially, it has been connecting to Postgres but for my purpose, it was overkill so I switched to SQLite which is faster to provision, develop and maintain in this small app. If you want to switch back to Postgres it's just changing a few lines in the dataSource config.
import{DataSource}from'typeorm';importdotenvfrom'dotenv';importBoardsfrom'./Boards';importCardsfrom'./Cards';importUsersfrom'./Users';importVotesfrom'./Votes';dotenv.config();constAppDataSource=newDataSource({type:'sqlite',database:'./db.sqlite',synchronize:true,logging:true,entities:[Boards,Cards,Users,Votes],subscribers:[],migrations:[],exportdefaultAppDataSource;Enter fullscreen modeExit fullscreen modeRETRO='retro',PLANNING_HIDDEN='planning_hidden',PLANNING_REVEALED='planning_revealed',@Entity()exportdefaultclassBoardsextendsBaseEntity{@PrimaryGeneratedColumn('uuid')id:string;@OneToMany(()=>Cards,(card)=>card.board)cards:Cards[];@OneToMany(()=>Users,(user)=>user.board)users:Users[];@Column({type:'integer',name:'stage',stage:number;@Column({type:'integer',name:'max_votes',maxVotes:number;@Column({type:'varchar',name:'mode',mode:string;@Column({name:'timer_to',timerTo:Date;@CreateDateColumn({name:'created_at',createdAt:Date;@UpdateDateColumn({name:'updated_at',updatedAt:Date;Enter fullscreen modeExit fullscreen mode
How I created AI-powered ORM for PostgreSQL, MySQL and SQLite and why you shouldn't use it
#webdev#programming#javascript
How to update a few years old outdated project in Node and React?
#webdev#javascript#tutorial#react
Data Structures using JavaScript for beginners
#javascript#beginners#webdev#tutorial
Built on Forem — the open source software that powers DEV and other inclusive communities.