簡體   English   中英

運行nodejs靜態網站

[英]Run nodejs static website

我是靜態網頁生成器和節點js的新手。 我遵循了此鏈接https://github.com/jnordberg/wintersmith 我已經成功創建並使用了一個項目

wintersmith build 

它已經創建了一個構建文件夾並創建了一些html文件。 問題是我也直接將其托管在IIS中,並通過在瀏覽器中打開.html文件來手動運行它。 但是它沒有加載正確的頁面,並且當我單擊頁面上的任何超級鏈接時,即使頁面可用,我也會得到404

我懷疑我必須在服務器上運行它。 但我不確定如何?

您可以使用此模塊 通過npm安裝並運行

http-server <path>

您的index.html所在文件夾的路徑在哪里。

            ## Create folder public within it and create three file with the name home.html, contact.html, about.html
            ## After that create a page web.js

            var http = require('http');
            var fs = require('fs');
            http.createServer( function(request,response) {
                var url = request.url;
                switch(url) {
                case '/':
                getStaticFileContent(response,'public/home.html','text/html');
                break;
                case '/about':
                getStaticFileContent(response,'public/about.html', 'text/html');
                break;
                case '/contact':
                getStaticFileContent(response,'public/contact.html','text/html');
                break;
                default:
                response.writeHead(404,{'Content-Type':'text/plain'});
                response.end('404-page not foud');
            }

            }).listen(4585);

            function getStaticFileContent(response, filepath, ContentType) {
                fs.readFile(filepath, function(error, data) {
                    if(error) {
                            response.writeHead(500,{'Content-Type':'text/plain'});
                            response.end('500- internal server Error');
                    } if(data) {
                        response.writeHead(200,{'ContentType':'text/html'});
                        response.end(data);
                    }
                });
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM