This article was last updated on 2018-01-19 at blog.sqdyy.cn
I. Content introduction
This article describes how to build a TypeScript development environment using Visual Studio Code, where we use Express, a flexible Web application development framework, to improve our coding efficiency. We will also add nodemon to automatically monitor changes to your source code and automatically restart the server. The purpose of writing this article is to leave a mark on the ground, but also hope to have a certain reference value for some beginners.
Before reading this article, you need to have some knowledge of Webpack and Typescript syntax. Reading this article, you will get started:
- Create the Web server using Nodejs
- Use Express to create restful HTTP services
- Use Nodemon to monitor server file changes and automatically restart the server
Build your WEB server
- So let’s create a
server
Folder and usenpm
Command, we developed our server using typescript language
npm init -y
Copy the code
- We need to introduce node type definition files. The purpose of using type definition files is to enable you to use existing javascript libraries in typescript
npm i @types/node --save
Copy the code
- Since nodejs itself does not recognize typescript directly, we need to compile typescript to javascript, so create the following tsconfig.json configuration file to tell the compiler how to compile typescript to javascript. For details, see the typescript official documentation:
{
"compilerOptions": {
"target": "es5"."module": "commonjs"."emitDecoratorMetadata": true."experimentalDecorators": true."outDir": "build"."lib": [
"es6"]},"exclude": [
"node_modules"]}Copy the code
-
We need to tell vscode to use this configuration file to compile our typescript, using the CTRL +shift+b shortcut to generate the solution.
-
Now that our development environment is configured, let’s start writing our server file. Let’s start by creating a server/hello_server.ts file.
import * as http from 'http';
const server = http.createServer((req,resp) => {
resp.end("Hello Node!");
});
server.listen(8000);
Copy the code
- Performed again
ctrl+shift+b
Compile this file, first execution will improve the lack.vscode/tasks.json
File, create it and continue executingctrl+shift+b
, according to thetsconfig.json
The configuration will be inbuild
Directory to generate compiled javascript code:
// build/hello_server.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var http = require("http");
var server = http.createServer(function (req, resp) {
resp.end("Hello Node!");
});
server.listen(8000);
Copy the code
- Now we can start our node server from this file by executing the following command to start the server and then access
localhost:8000
:
node build/hello_server.js
Copy the code
Use the Express framework to simplify development
Express is a fast, open and minimalist Web development framework based on the Node.js platform. The reason for using it is simple. We use the original Node for development, which requires us to handle many problems manually, such as reading files, routing requests, and handling different types of requests. Using Express can help you do these things faster. So you should go to its website and learn about it.
- First we install
express
Framework:
npm install express --save
Copy the code
- And then we introduce
express
Type definition file for:
npm install @types/express --save
Copy the code
- Now that we can use the express API with typescript code, we create a new server configuration file
server/auction_server.ts
:
import * as express from 'express'; const app = express(); // Declare a service that handles GET requests app.get()'/', (req, resp) => {
resp.send("Hello Express");
});
app.get("/products", (req, resp) => {
resp.send("Commodity query request received");
});
const server = app.listen(8000, "localhost", () => {
console.log("The server is started at http://localhost:8000");
});
Copy the code
- Now let’s implement
ctrl+shift+b
And then throughauction_server.js
File to start node server:
node build/auction_server.js
Copy the code
Use the Nodemon tool to automatically reload the server
We have learned to build a node server, but it is very inconvenient, and the server does not automatically restart when we change the project code. This is very annoying and a waste of time, so someone developed nodemon, a tool that automatically reboots. Let’s install it.
1. First we install Nodemon:
npm install -g nodemon
Copy the code
2. Run the following command to start the server:
nodemon build/auction_server.js
Copy the code
If you’re interested, see this article: Building nodeJS of typescript with VSCode to automatically compile and start the service