更改 Node.js 的 npm 啟動指令碼
Node.js 應用程式的啟動指令碼包含了執行特定任務所需的所有命令。在啟動或初始化 Node.js 專案時,會建立許多預定義的指令碼用於執行應用程式。可以根據專案的需要或需求更改這些指令碼。
指令碼命令廣泛用於在 Node 和 React 中建立程式的不同啟動指令碼。“npm start”用於執行啟動指令碼,而無需鍵入其執行命令。
package.json 檔案
這是需要新增到 package.json 檔案中的啟動指令碼。此處的檔名是“index.js”。您可以根據需要更改此名稱。
"scripts"{ "start":"node index.js" //Name of the file }
下面是一個展示啟動指令碼實現的示例。
示例 - sendStatus()
建立一個名為 index.js 的檔案並複製下面的程式碼片段。建立檔案後,使用以下命令執行此程式碼,如以下示例所示:
npm start
index.js
// Importing the express module const express = require('express'); const app = express(); // Initializing the data with the following string var data = "Welcome to TutorialsPoint !" // Sending the response for '/' path app.get('/' , (req,res)=>{ // Sending the data json text res.send(data); }) // Setting up the server at port 3000 app.listen(3000 , ()=>{ console.log("server running"); });
輸出
C:\home
ode>> npm start
上述語句將自行執行 index.js 檔案。此命令本身啟動了專案的 main 類。
現在,從瀏覽器中訪問以下 URL 以訪問網頁:https://:3000
廣告