美文网首页
Node创建服务器

Node创建服务器

作者: 竹剑道 | 来源:发表于2017-07-11 00:56 被阅读27次

    不学node的前端不是好流氓。

    原生搭建简单文件服务器

    'use strict';
    
    const fs = require('fs'),
        url = require('url'),
        path = require('path'),
        http = require('http');
    
    const root = path.resolve(process.argv[2] || '.');
    console.log('root' + root);
    
    const server = http.createServer((request, response) => {
        const pathname = url.parse(request.url).pathname;
        const filepath = path.join(root, pathname);
        console.log(filepath);
        fs.stat(filepath, (err, stats) => {
            if (pathname === '/') {
                const indexPath = path.join(root, 'index.html');
                const defaultPath = path.join(root, 'default.html');
                fs.stat(indexPath, function (err, stat) {
                    if (err) {
                        fs.stat(defaultPath, function (err, stat) {
                            if (err) {
                                console.log('can\'t find flie!');
                                response.writeHead(404, { 'Content-Type': 'text/html' });
                                response.end('<p>404 Not Found</p>')
                            } else {
                                console.log('file is exist!');
                                response.writeHead(200);
                                fs.createReadStream(defaultPath).pipe(response);
                            }
                        });
                    } else {
                        console.log('file is exist!');
                        response.writeHead(200);
                        fs.createReadStream(indexPath).pipe(response);
                    }
                });
            } else {
                if (!err && stats.isFile()) {
                    console.log(`200 ${request.url}`);
                    response.writeHead(200);
                    fs.createReadStream(filepath).pipe(response);
                } else {
                    console.log(`404 ${request.url}`);
                    response.writeHead(404);
                    response.end('404 Not Found');
                }
            }
        })
    
    });
    
    server.listen(8090);
    console.log('server is running at http://127.0.0.1:8090/');
    

    代码分析

    • path.resolve([...paths])

    path.resolve([...paths])#
    Added in: v0.3.4
    ...paths: <String> A sequence of paths or path segments
    Returns: <String>

    The path.resolve() method resolves a sequence of paths or path segments into an absolute path.
    The given sequence of paths is processed from right to left, with each subsequent path
    prepended until an absolute path is constructed. For instance, given the sequence of path segments: /foo, /bar, baz,** calling path.resolve('/foo', '/bar', 'baz')** would <b>return /bar/baz </b>.
    If after processing all given path segments an absolute path has not yet been generated, the current working directory is used.
    The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.
    Zero-length path segments are ignored.
    If no path segments are passed, path.resolve() will return the absolute path of the current working directory.
    For example:

    path.resolve('/foo/bar', './baz');  // Returns: '/foo/bar/baz';
    path.resolve('/foo/bar', '/tmp/file/'); // Returns: '/tmp/file';
    path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
    // if the current working directory is /home/myself/node;
    // this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'
    

    A TypeError
    is thrown if any of the arguments is not a string.
    通过官方文档可知 path.resolve()是用来解析合成绝对路径的。

    • process.argv

    Added in: v0.1.27
    <Array>

    The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.
    For example, assuming the following script for process-args.js:

    // print process.argv
    process.argv.forEach((val, index) => {
      console.log(`${index}: ${val}`);
    });
    

    Launching the Node.js process as:

    $ node process-2.js one two=three four
    

    Would generate the output:

    0: /usr/local/bin/node
    1: /Users/mjr/work/node/process-2.js
    2: one
    3: two=three
    4: four
    

    总结: 这个是返回一个数组,第一个元素是node的执行文件目录** 的路径,第二个元素是js执行文件** 的路径,剩下的元素可以是任何 命令行 的参数。

    • http.createServer([requestListener])#

    Added in: v0.1.13
    Returns: <http.Server>

    Returns a new instance of http.Server.
    The requestListener is a function which is automatically added to the 'request'
    event.

    • server.listen([port][, hostname][, backlog][, callback])#

    Added in: v0.1.90
    port <Number>
    hostname <String>
    backlog <Number>
    callback <Function>

    Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise. Omit the port argument, or use a port value of 0, to have the operating system assign a random port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.
    To listen to a unix socket, supply a filename instead of port and hostname.
    backlog is the maximum length of the queue of pending connections. The actual length will be determined by your OS through sysctl settings such as tcp_max_syn_backlog and somaxconn on linux. The default value of this parameter is 511 (not 512). This function is asynchronous. callback will be added as a listener for the 'listening' event. See also net.Server.listen(port).
    Note: The server.listen() method may be called multiple times. Each subsequent call will re-open the server using the provided options.
    总结:这就是用来监听端口的。奇怪的是如果端口为0或者没传,操作系统会随机分配一个端口。

    • url.parse(urlString[, parseQueryString[, slashesDenoteHost]])#

    Added in: v0.1.25
    urlString <String> The URL string to parse.
    parseQueryString <Boolean> If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. Defaults to false.
    slashesDenoteHost <Boolean> If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. Defaults to false.
    The url.parse() method takes a URL string, parses it, and returns a URL object.
    这个是用来解析URL字符串的,解析成 URLobject.

    • path.join([...paths])

    Added in: v0.1.16
    ...paths <String> A sequence of path segments
    Returns: <String>
    For example:

    path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
    // Returns: '/foo/bar/baz/asdf'
    
    path.join('foo', {}, 'bar')
    // throws TypeError: Arguments to path.join must be strings
    
    

    总结:这是用来合成路径的。

    • fs.stat(path, callback)

    Added in: v0.0.2
    path<String> | <Buffer>
    callback <Function>
    总结: 它返回一个Stat对象,能告诉我们文件或目录的详细信息

    • fs.createReadStream(path[, options])

    总结: 打开创建一个可读流。

    • fs.createWriteStream(path[, options])

    总结: 打开创建一个可写流。所有可以读取数据的流都继承自stream.Readable,所有可以写入的流都继承自stream.Writable。

    • pipe()

    一个Readable流和一个Writable流串起来后,所有的数据自动从Readable流进入Writable流,这种操作叫pipe。

    总结

    这是原生node搭建的一个文件读取服务器,基本读取功能齐全,能做静态网页(简单的官网等)的应用,代码不是很难,稍微了解node的人都能很好的理解。重点是了解和熟悉node API 和 编程思想,为以后真正应用做基础。 在命令行运行 ** node xxx.js(服务器js) /path/to/dir**,把 **/path/to/dir **改成你本地的一个有效的目录。

    相关文章

      网友评论

          本文标题:Node创建服务器

          本文链接:https://www.haomeiwen.com/subject/krtphxtx.html