
The following example demonstrates how to serve JSON response from the Node.js web server. npx tsc Our compiled code Check out build/index.js, weve compiled our first TypeScript file. tsc will read the tsconfig.json in the current directory, and apply the configuration against the TypeScript compiler to generate the compiled JavaScript code. It will display "Invalid Request" for all requests other than the above URLs. To compile our code, well need to run the tsc command using npx, the Node package executer. Open source developers from every continent use npm to share and borrow packages, and many organizations. npm is the world’s largest software registry. The same way, point your browser to and see the following result. To start with Node you need to install npm. To test it, you can use the command-line program curl, which most Mac and Linux machines have pre-installed.Ĭurl -i You should see the following response.įor Windows users, point your browser to and see the following result. Node.js web server at port 5000 is running. Now, run the above web server as shown below. Finally, Node.js web server sends the response using end() method. To send a response, first it sets the response header using writeHead() method and then writes a string as a response body using write() method. Creating Node.js Project using NPM by Niluka Sripali Monnankulama Many Minds Medium Write Sign up Sign In 500 Apologies, but something went wrong on our end. In the above example, req.url is used to check the url of the current request and based on that it sends the response.
