简体   繁体   English

运行nodejs静态网站

[英]Run nodejs static website

I am new to static web page generators and node js. 我是静态网页生成器和节点js的新手。 I have followed this link https://github.com/jnordberg/wintersmith . 我遵循了此链接https://github.com/jnordberg/wintersmith I have successfully created and build a project using 我已经成功创建并使用了一个项目

wintersmith build 

It has created a build folder and created some html files. 它已经创建了一个构建文件夹并创建了一些html文件。 The problem is i have hosted this directly in IIS also and run it manully by opening the .html files in browser. 问题是我也直接将其托管在IIS中,并通过在浏览器中打开.html文件来手动运行它。 But it is not loading the proper page and when i click any hyper link on the page i am getting 404 eventhough the pages are available. 但是它没有加载正确的页面,并且当我单击页面上的任何超级链接时,即使页面可用,我也会得到404

I suspect that i have to run this in a server. 我怀疑我必须在服务器上运行它。 But i am not sure how? 但我不确定如何?

You can use this module . 您可以使用此模块 Install it via npm and simply run 通过npm安装并运行

http-server <path>

where is the path to the folder where your index.html is located. 您的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