Node.js用6行代碼1個JS文件搭建一個HTTP靜態服務器

Node.js宣言:Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. node

大概意思:Node.js是一個基於Chrome的JavaScript運行時的用戶以輕鬆構建快速、可擴展的網絡應用平臺。 Node.js使用事件驅動、非阻塞I/ O模型,使它輕量級、高效和完美的適用於運行在分佈式設備上的數據密集型的實時應用程序。 git

學習Node.js有2周了,本身也開發了一個開源的基於Bootstrap的應用OSN(https://github.com/obullxl/osnode-site),能輕鬆運行在免費百度雲引擎上(http://obullxl.duapp.com/);如今總結一下,把學習過程當中的知識點總結一下。 github

6行代碼1個JS文件輕鬆搭建HTTP服務器 express

var http = require('http');
var express = require('express');
var app = express();
app.use("/public", express.static(__dirname + '/public'));

// 建立服務端
http.createServer(app).listen('80', function() {
	console.log('啓動服務器完成');
});


使用方法: npm

一、把該數據內容複製到一個JS文件中,如「app.js」; 瀏覽器

二、把「app.js」文件放到須要做爲服務器的目錄下,文件發到「public」目錄下; 服務器

三、運行命令「node app.js」,若沒有安裝express模塊,運行命令「npm install express」進行安裝; 網絡

四、打開瀏覽器,訪問服務器:http://127.0.0.1/public/文件名 app

相關文章
相關標籤/搜索