Posts

NODE.JS Create an HTTP Server

  LEARN NODE.JS Create an HTTP Server Node was designed with back end development needs as a top priority. One of these needs is the ability to create web servers, computer processes that listen for requests from clients and return responses. A Node core module designed to meet these needs is the  http  module. This module contains functions which simplify interacting with HTTP and streamline receiving and responding to requests. The  http.createServer()  method returns an instance of an  http.server . An  http.server  has a method  .listen()  which causes the server to “listen” for incoming connections. When we run  http.createServer()  we pass in a custom callback function (often referred to as the  requestListener ). This callback function will be triggered once the server is listening and receives a request. Let’s break down how the  requestListener  callback function works: The function expects two arguments: a request object and a response object. Each time a request to the serv

Writable Streams

  LEARN NODE.JS Writable Streams In the previous exercise, we were reading data from a stream, but we can also write to streams! We can create a writeable stream to a file using the  fs.createWriteStream()  method: const fs = require ( 'fs' ) const fileStream = fs . createWriteStream ( 'output.txt' ); fileStream . write ( 'This is the first line!' ); fileStream . write ( 'This is the second line!' ); fileStream . end (); In the code above, we set the output file as  output.txt . Then we  .write()  lines to the file. Unlike a readable stream, which ends when it has no more data to read, a writable stream could remain open indefinitely. We can indicate the end of a writable stream with the  .end()  method. Let’s combine our knowledge of readable and writable streams to create a program which reads from one text file and then writes to another.

LEARN NODE.JS Readable Streams

LEARN NODE.JS Readable Streams In the previous exercise, we practiced reading the contents of entire files into our JavaScript programs. In more realistic scenarios, data isn’t processed all at once but rather sequentially, piece by piece, in what is known as a  stream . Streaming data is often preferable since you don’t need enough RAM to process all the data at once nor do you need to have all the data on hand to begin processing it. One of the simplest uses of streams is reading and writing to files line-by-line. To read files line-by-line, we can use the  .createInterface()  method from the  readline  core module.  .createInterface()  returns an  EventEmitter  set up to emit  'line'  events: const readline = require ( 'readline' ); const fs = require ( 'fs' ); const myInterface = readline . createInterface ({ input : fs . createReadStream ( 'text.txt' ) }); myInterface . on ( 'line' , ( fileLine ) => { console . log ( `The

AI TIC TAC TOE

Image
  In today’s article, I am going to show you how to create an unbeatable AI agent that plays the classic  Tic Tac Toe  game. You will learn the concept of the  Minimax  algorithm that is widely and successfully used across the fields like  Artificial Intelligence ,  Economics ,  Game Theory ,  Statistics  or even  Philosophy . AI  X  vs Human  O Table of Contents About Tic Tac Toe Minimax algorithm Unbeatable Tic Tac Toe AI Bonus What’s next? Before we go into the AI part, let’s make sure that we understand the game. About Tic Tac Toe Tic-tac-toe  (also known as  noughts and crosses  or  Xs and Os ) is a  paper-and-pencil game  for two players,  X  and  O , who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. I recommend you to play the game yourself, feel free to check out my  iOS Tic Tac Toe  app. ‎Beat me XO ‎Read reviews, compare customer ratings, see screenshots, and lear