nodejs 構建本地web測試伺服器 以及 解決訪問靜態資源的問題!

來源:互聯網
上載者:User

標籤:creat   .com   模板檔案   指定   stat   nal   修改   文檔   logs   

直接開啟html檔案,是以file:///方式開啟的,這種方式很多時候會遇到跨域的問題,因此我們一般會搭建一個簡易的本機伺服器,來運行測試頁面。

一、構建靜態伺服器

1、使用express模組

建立個js檔案,命名server,內容代碼如下:

 1 var express = require(‘express‘); 2 var app = express(); 3 var path = require(‘path‘); 4  5 //指定靜態資源訪問目錄 6 app.use(express.static(require(‘path‘).join(__dirname, ‘public‘))); 7 // app.use(express.static(require(‘path‘).join(__dirname, ‘views‘))); 如果有檔案夾存放資源,出現報錯的話,那就多use幾次就可以了 8 // 設定views變數,意為視圖存放的目錄 9 app.set(‘views‘, (__dirname + "/public"));10 // app.set(‘views‘, __dirname);11 // 修改模板檔案的尾碼名為html12 app.set( ‘view engine‘, ‘html‘ );13 // 運行ejs模組14 app.engine( ‘.html‘, require( ‘ejs‘ ).__express );15 16 app.get("/", function(req, res) {17     res.render(‘index‘);18 });19 20 var server = app.listen(1336, "127.0.0.1",function(){21     var host = server.address().address;22     var port = server.address().port;23     console.log("Server running at http://%s:%s", host, port)24 });

檔案結構如下:

啟動並執行話只要執行:node server.js 就可以了

然後在瀏覽器輸入 http://127.0.0.1:1336/ 來訪問專案檔夾內的檔案了

2、使用connect模組

建立個js檔案,命名 server2 ,內容代碼如下:

 var connect = require("connect"); var serveStatic = require("serve-static"); var app = connect(); // app.use(serveStatic("C:\\xxx\\xxx\\xxx\\專案檔夾")); app.use(serveStatic("public")); app.listen(1337); console.log(‘Server running at http://127.0.0.1:1337/‘);

啟動並執行話只要執行:node server2.js 就可以了,

然後在瀏覽器輸入 http://127.0.0.1:1337/ 來訪問專案檔夾內的檔案了。(如果是index.html檔案可以省略不寫,預設載入的就是這個檔案);

3、使用http模組

建立個js檔案,命名 server3 ,內容代碼如下:

 1 var finalhandler = require(‘finalhandler‘) 2 var http = require(‘http‘) 3 var serveStatic = require(‘serve-static‘) 4  5 // Serve up public/ftp folder 6 var serve = serveStatic(‘public‘, {‘index‘: [‘index.html‘, ‘index.htm‘]}) 7  8 // Create server 9 var server = http.createServer(function onRequest (req, res) {10     serve(req, res, finalhandler(req, res))11 })12 13 // Listen14 server.listen(1338);15 console.log(‘Server running at http://127.0.0.1:1338/‘);

啟動並執行話只要執行:node server3.js 就可以了,

然後在瀏覽器輸入 http://127.0.0.1:1338/ 來訪問專案檔夾內的檔案了。

註:總的檔案目錄如下:

源碼:https://github.com/arvin0/nodejs-example/tree/master/web-static-test-server

二、解決訪問靜態資源

主要使用兩個模組

1.通用的 serve-static 模組

詳細文檔:https://github.com/expressjs/serve-static

2.express專屬的  app.use(express.static(require(‘path‘).join(__dirname, ‘public‘)));  方法

詳細文檔:http://expressjs.com/en/4x/api.html ,然後ctrl+F搜尋 express.static ,就能找到對應的說明了。

 

nodejs 構建本地web測試伺服器 以及 解決訪問靜態資源的問題!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.