WebStorm 簡單搭建NodeJs服務

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

 

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

 

 

 

開始 編寫 app,js web

// 引入 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 服務的 ),那還缺乏一個 啓動的.....express

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

 

// 引入自已的模塊
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 框架

 

 

 

 

 

 

 

 

 

 

 

 

 

啓動完成後 看 控制檯:函數

 

 

 

進行訪問:post

 

 

 

 

 

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

相關文章
相關標籤/搜索