运行nodejs静态网站

时间:2016-05-05 14:07:59

标签: javascript node.js

我是静态网页生成器和节点js的新手。我已关注此链接https://github.com/jnordberg/wintersmith。我已经使用

成功创建并构建了一个项目
wintersmith build 

它创建了一个构建文件夹并创建了一些html文件。问题是我也直接在IIS中托管它,并通过在浏览器中打开.html文件来手动运行它。但它没有加载正确的页面,当我点击页面上的任何超链接时,我得到404尽管页面可用。

我怀疑我必须在服务器中运行它。但我不知道怎么样?

2 个答案:

答案 0 :(得分:1)

您可以使用this module。通过npm安装它,然后运行

http-server <path>

index.html所在文件夹的路径在哪里。

答案 1 :(得分:-2)

            ## 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);
                    }
                });
            }
相关问题