本文創建web服務器語言環境:
- TypeScript
- WebStrom
創建web服務器詳細步驟: |
1、在根目錄下初始化文件夾,命令:
npm init -y
2、應用node類型定義文件,作用爲了讓開發者可以在ts中使用已有的js庫,命令
npm i @types/node --save
3、需要將ts編譯成js,在根目錄下新建配置文件,命名爲:tsconfig.json
{
"compilerOptions": { //編譯配置器
"target":"es5", //目標是編譯成es5規範的腳本,也就是js
"module": "commonjs", //模塊的規範是commonjs
"emitDecoratorMetadata":true,
"experimentalDecorators":true,//這來給你個是要保存呢裝飾器的源數據
"outDir":"build",//編譯後文件默認放在build文件夾下
"lib":["es6"]//開發時使用es6的語法
},
"exclude": [
"node_modules"
]
}
4、點擊WebStorm菜單欄的File->Setting
5、在根目錄下新建 server 文件夾,新建ts文件
import * as http from 'http'
const server = http.createServer((request, response) => {
response.end("Hello Node!");
});
server.listen(8000);
6、此時,根目錄會自動生成build文件夾,其中有編譯好的Js文件
7、啓動node服務器,命令
node build/js文件名
8、此時,訪問路徑:http://localhost:8000(在ts文件中設置服務器端口號爲8000)
9、安裝Express框架,此框架提供了web應用常用的的功能,命令:
npm install express --save
10、安裝express類型定義文件,命令:
npm install @types/express --save
11、每當修改ts文件的內容時,都要重新啓動node服務器,數據才能更新。安裝nodemon工具,可以解決這一問題。當代碼變動時,nodemon工具會自動加載代碼,不用重新啓動服務器。命令:
npm install -g nodemon
12、安裝完nodemon工具,需要使用nodemon啓動一次服務器,命令:
nodemon build/js文件名
13、試試修改代碼,再刷新網頁。
以上就是創建web服務器的詳細步驟,感謝閱讀。