Node.js is a powerful JavaScript runtime that allows developers to build scalable server-side applications. When combined with TypeScript, a strongly-typed superset of JavaScript, we gain advantages like type safety and better tooling. In this guide, we’ll set up a basic Node.js application with TypeScript, paving the way for building a CRUD application with MongoDB.
Before we begin, ensure that you have the following installed on your local environment:
node -v
in your terminal)First, we need to create a new directory for our project.
mkdir node-typescript-app cd node-typescript-app
Next, initialize a new Node.js project:
npm init -y
This command generates a package.json
file with default settings.
To use TypeScript, we need to install it as a development dependency.
npm install typescript --save-dev
After installing TypeScript, we’ll need to create a TypeScript configuration file (tsconfig.json
) to define the compiler options for our project.
npx tsc --init
This generates a tsconfig.json
file with default options. You can adjust some settings according to your project needs. Here’s a recommended configuration to get started:
{ "compilerOptions": { "target": "ES6", "module": "commonjs", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "outDir": "./dist" }, "include": ["src/**/*"], "exclude": ["node_modules"] }
Now, we’ll create the necessary folder structure. This helps in organizing our files.
mkdir src
Inside the src
directory, create a file named index.ts
. This will serve as the main entry point for our application.
touch src/index.ts
Let’s add some basic code to index.ts
. Open it in your favorite code editor and add the following code.
import http from 'http'; const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, TypeScript and Node.js!'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
To run our application, we need to compile the TypeScript code into JavaScript. Use the following command:
npx tsc
This will create a dist
folder containing the compiled JavaScript code based on the tsconfig.json
settings.
You can run the compiled JavaScript using Node.js:
node dist/index.js
If everything is set up correctly, you’ll see the message Server running at http://127.0.0.1:3000/
. Open your web browser and navigate to that address. You should see the response Hello, TypeScript and Node.js!
.
To streamline our development process, let’s set up some scripts in our package.json
. We’ll add a script to compile TypeScript and start the server in one go.
Edit your package.json
and add the following scripts:
"scripts": { "build": "tsc", "start": "node dist/index.js", "dev": "tsc -w" }
build
: Compiles the TypeScript files to JavaScript in the dist
folder.start
: Runs the application.dev
: Runs TypeScript in watch mode, which compiles files automatically when they are changed.You can now run:
npm run build npm start
Or, for development mode:
npm run dev
Congratulations, you now have a basic Node.js application written in TypeScript! This sets the stage for further development, such as implementing a RESTful API, connecting to a MongoDB database for data storage, and adding more complex routes and middleware to handle various requests.
By utilizing TypeScript, you’ll benefit from features like type-checking and better IDE support, enhancing your overall development experience. As you continue to learn, consider diving deeper into Node.js frameworks like Express, and exploring how TypeScript can provide better structure and safety in larger applications.
31/08/2024 | NodeJS
08/10/2024 | NodeJS
14/10/2024 | NodeJS
31/08/2024 | NodeJS
31/08/2024 | NodeJS
14/10/2024 | NodeJS
14/10/2024 | NodeJS
14/10/2024 | NodeJS
14/10/2024 | NodeJS
14/10/2024 | NodeJS