Install Node.js runtime environment to run JavaScript on your server, outside a browser. It's necessary for using packages like Express.js and for running your server-side JavaScript or TypeScript code.
Download the installer appropriate for your system. You'll see options for Windows and macOS.
Once the installer is downloaded, run it and follow the prompts in the installation process.
Linux:
You can use a package manager to install Node.js. For Ubuntu, the package manager is apt. Here's how you can install Node.js:
bashCopy code
sudo apt update
sudo apt install nodejs
You will also want to install npm (Node Package Manager), which is used to install Node.js packages. It's usually installed with Node.js, but if it's not, you can install it with:
bashCopy code
sudo apt install npm
Verify Installation:
To ensure that Node.js and npm are installed, you can check their versions. Open a new terminal/command prompt window and type:
bashCopy code
node --version
npm --version
To switch from JavaScript to TypeScript, we need to make some modifications to our code and setup:
Step 1: Install TypeScript
You can install TypeScript globally by using:
bashCopy code
npm install -g typescript
Or you can install it as a development dependency in your project:
bashCopy code
npm install --save-dev typescript
Step 2: Install Typings
You'll also need typings for Node.js and Express.js to provide type checking and IntelliSense:
bashCopy code
npm install --save-dev @types/node @types/express
Step 3: Create a tsconfig.json
You'll need a tsconfig.json in your project root to set up your TypeScript configuration. Here's a basic one:
jsonCopy code
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
},
"include": ["./src/**/*.ts"],
"exclude": ["node_modules"]
}
This configuration will output the transpiled JavaScript files into a directory named dist and will process TypeScript files located in the src directory.
Step 4: Convert server1.js to server1.ts
Now, let's rewrite the server1.js file into TypeScript. Note that the modifications in the code are mostly type annotations and the logic remains unchanged:
const index = books.findIndex((book) => book.id === id);
if (index !== -1) {
books.splice(index, 1);
res.send();
} else {
res.status(404).send();
}
});
app.listen(3001, () => console.log('Server running on port 3001'));
Step 5: Transpile TypeScript
In your project root directory, run:
bashCopy code
tsc
This command transpiles your TypeScript files into JavaScript files, as specified in your tsconfig.json.
Step 6: Run Your Server
Since our tsconfig.json outputs the JavaScript files into dist directory, you can start the server with:
bashCopy code
node dist/server1.js
Optional: Setting Up Nodemon and ts-node for Development
For development, you might want your server to restart automatically every time you make changes. We can use nodemon and ts-node for this. ts-node is a TypeScript execution environment for Node.js, and nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your Node application.