基本的HTTP服务器瓦特/节点JS

我有点困难。 我是一般的新手,这是我本周为JavaScript课程分配的程序。 我们应该创build一个Web服务器,用于从文件系统提供文件,也可以实现Node模块。 它必须能够读取HTML,CSS,GIF和JS资源。 这些文件将被放置在与脚本相同的文件夹下,位于/ scripts和/ html目录下。 这里是我试图实现的逻辑,但是我的terminal不断告诉我path.extname(资源)是一个未定义的函数。 我知道这很难看,但是我仍然试图围绕这个概念。 任何帮助都会很好,我一直在想这一天过去。

'use strict'; // Load the file system module var fs = require('fs'); // load the http module var http = require('http'); // load the url module var url = require('url'); // load the path module var pathModule = require('path'); // The following function will be called when the server // is handling a request function servePage(request, response) { // get path name and remove '/' var path = url.parse(request.url).pathname.substring(1); // if path is an empty string if (path === '') { path = './html/home.html'; } // Read the file asynchronously; HTML fs.readFile(path, function (err, content) { var extension = path.extname(path); switch (extension){ case '.hmtl': response.writeHead(200, {'Content-Type': 'image/gif'}); response.write(content); // Send file contents as response body response.end(); break; case '.gif': response.writeHead(200, {'Content-Type': 'text/html; charset = UTF-8'}); response.write(content); // Send file contents as response body response.end(); break; case '.css': response.writeHead(200, {'Content-Type': 'text/css; charset = UTF-8'}); response.write(content); // Send file contents as response body response.end(); break; case '.js': response.writeHead(200, {'Content-Type': 'application/javascript; charset = UTF-8'}); response.write(content); // Send file contents as response body response.end(); break; case 'err': response.writeHead(404, {'Content-Type': 'text/plain; charset = UTF-8'}); response.write(err.message + ' - The page requested was not found.'); response.end(); // Done break; } /* } if (err) { // If there is an error, set the status code response.writeHead(404, {'Content-Type': 'text/plain; charset = UTF-8'}); response.write(err.message + ' - The page requested was not found.'); response.end(); // Done // else if the content was succesfully read } else { response.writeHead(200, {'Content-Type': 'text/html; charset = UTF-8'}); response.write(content); // Send file contents as response body response.end(); } */ }); } // create a server object var server = http.createServer(servePage); server.listen(8080); console.log('Server running at http://localhost:8080'); 

这是我将如何重构你的代码。

 'use strict'; var fs = require('fs'); var http = require('http'); var url = require('url'); var path = require('path'); function getContentType(ext) { switch (ext.toLowerCase()) { case '.html': return 'text/html; charset=UTF-8'; case '.gif': return 'image/gif'; case '.css': return 'text/css; charset=UTF-8'; case '.js': return 'application/javascript; charset=UTF-8'; default: return 'text/plain; charset=UTF-8'; } } var server = http.createServer(function (request, response) { // This is dangerous. var filePath = url.parse(request.url).pathname.substring(1); if (filePath === '') { filePath = './html/home.html'; } fs.readFile(filePath, function (err, content) { var statusCode = 200, headers = { 'Content-Type': getContentType(path.extname(filePath)) }; if (err) { statusCode = 404; headers['Content-Type'] = getContentType('.txt'); content = 'The page requested was not found.\n\n' + err.message; } response.writeHead(statusCode, headers); response.write(content); response.end(); console.log('' + statusCode + ': ' + filePath); }); }); server.listen(8080); console.log('Server running at http://localhost:8080'); 

请注意这一点

 var filePath = url.parse(request.url).pathname.substring(1); 

是危险的 想想当有人请求/../../foo/bar时会发生什么。

请使用快速框架,这将使您的工作变得简单。

 var express = require('express'); app.use("/", express.static(__dirname + '/client/')); app.listen(3000,function(){ console.log("App Started on PORT 3000"); }); 

这里你的客户端文件夹将包含所有的文件,即gif,html等

你的pathvariables被分配给一个string,而你的'path'模块被分配给一个pathModule

所以没有函数调用path (因为它是一个string)

您应该将其更改为pathModule.extname() ;