WebStorm 簡單搭建NodeJs服務


開始使用 WebStorm 搭建( WebStorm 請自行安裝...... )

 

 在 項目 根目錄 新建個 app.js 

 

 

 

開始 編寫 app,js 

// 引入 HTTP 模塊
const http = require("http");
// 可以使用 HTTPS 模塊
// const https = require("https");

var httpService = function (app,port) {
    // 創建 node 服務
    // 如果 使用 https 的話 還需要 證書
    var httpService = http.createServer(app).listen(port);
    // 監聽服務
    httpService.on('listening',onListening);
    // 監聽函數
    function onListening() {
        var addr = httpService.address();
        var bind = typeof addr === 'string'
            ? 'pipe ' + addr
            : 'port ' + addr.port;
        console.log('Listening on ' + bind);
    }
}

// 模塊導出
module.exports = httpService;

 

 

 

app.js ( 這里 我專門是用來寫創建 nodeJs 服務的 ),那還缺少一個 啟動的.....

同樣也是在根目錄 新建個  start,js 文件

 

// 引入自已的模塊
const start = require('./app');
// 引入 express 模塊
const express = require('express');

// 使用 express 極簡的web開發框架
// 具體搜官方
var app = express();

// 你可以這樣使用:
// app.use
// app.post
// app.get
// app.delete
app.use(function (req, res, next) {
    res.writeHead(200,{"Content_Type":"text/html"});//設置響應格式
    res.write("hello NodeJS");
    res.end();
});

// 啟動服務
start(app, 8020);

 

 

現在來啟動 這個 start.js 

 

 

 

 

 

 

 

 

 

 

 

 

 

啟動完成后 看 控制台:

 

 

 

進行訪問:

 

 

 

 

 

這樣 就完成了一個 簡單的 nodeJs 服務搭建

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM