zoukankan      html  css  js  c++  java
  • Nodejs随笔(一):Hello World!

    声明:本人用的是Ubuntu 14.04 LTS 系统。

    一、Nodejs安装:

      <1>直接apt-get安装,最简单:sudo apt-get install nodejs

      <2>编译安装

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install build_essential openssl libssl-dev pkg-config
    wget http://nodejs.org/dist/v0.12.2/node-v0.12.2-linux-x64.tar.gz
    tar -zxvf node-v0.12.2-linux-x64.tar.gz
    cd node-v0.12.2-linux-x64
    ./configure
    make
    sudo make install

      验证是否安装成功:

    mesogene@mesogene-team:~/nodejs-workspace/01$ node -v
    v0.12.1
    mesogene@mesogene-team:~/nodejs-workspace/01$ npm -version
    2.5.1

    参考https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

    二、Hello World程序

    mesogene@mesogene-team:~/nodejs-workspace/01$ cat app.js 
    var http = require('http');
    http.createServer(function(req, res){
        // content header
        res.writeHead(200, { 'Content-Type':'text/html' });
        // write message adn signal communication is complete
        res.end('Hello World!');
    }).listen(process.env.PORT || 8124);
    // }).listen(8124);
    
    console.log('Server running on 8124');

      使用node app.js后,可在浏览器地址栏输入:http://localhost:8124/ 以查看结果。

      如果直接在命令行使用node app.js,它会占用我一个命令行窗口。有时,我不想其占用那个窗口,我们可以将node程序转为后台运行,那就的使用nohup命令来帮忙了。用法如下:

    mesogene@mesogene-team:~/nodejs-workspace/01$ nohup node app.js &
    [2] 7451
    [1]   Terminated              nohup node app.js > ./log
    mesogene@mesogene-team:~/nodejs-workspace/01$ nohup: ignoring input and appending output to ‘nohup.out’
    
    mesogene@mesogene-team:~/nodejs-workspace/01$ 

      或者:将输入重定向到当前目录的log文件中。

    mesogene@mesogene-team:~/nodejs-workspace/01$ nohup node app.js > ./log &
    [1] 7422
    mesogene@mesogene-team:~/nodejs-workspace/01$ nohup: ignoring input and redirecting stderr to stdout
    
    mesogene@mesogene-team:~/nodejs-workspace/01$ cat log 
    Server running on 8124

      问题来了,我如何杀死正在运行的node app.js呢?

      当然,需借助GNU/Linux shell命令的帮忙:ps和kill(nohup也是GNU/Linux shell命令)。

    mesogene@mesogene-team:~/nodejs-workspace/01$ ps -ef | grep node
    root      2830     1  0 09:53 ?        00:00:00 /sbin/mount.ntfs /dev/sdc2 /media/mesogene/Richard -o rw,nosuid,nodev,uhelper=udisks2,uid=1000,gid=1000,dmask=0077,fmask=0177
    root      2875     1  0 09:53 ?        00:00:00 /sbin/mount.ntfs /dev/sdc1 /media/mesogene/Richard1 -o rw,nosuid,nodev,uhelper=udisks2,uid=1000,gid=1000,dmask=0077,fmask=0177
    mesogene  3628  3590  0 09:54 pts/0    00:00:00 node
    mesogene  7451  7244  0 10:30 pts/27   00:00:00 node app.js
    mesogene  7468  7244  0 10:32 pts/27   00:00:00 grep --color=auto node
    mesogene@mesogene-team:~/nodejs-workspace/01$ kill 7451
    mesogene@mesogene-team:~/nodejs-workspace/01$ 

    扩展:ps与kill命令

    一、ps命令比较复杂,用Unix、BSD和GNU三种使用风格,下面主要列出简单用法,下一次单独写ps用法。以下是ps的两种常用方法:

      ps -ef(Unix风格),UID代表:启动进程的用户、PID:进程号、PPID:父进程号、C:进程生命周期CPU利用率...

    mesogene@mesogene-team:~$ ps -ef 
    UID        PID  PPID  C STIME TTY          TIME CMD
    root         1     0  0 09:52 ?        00:00:01 /sbin/init
    root         2     0  0 09:52 ?        00:00:00 [kthreadd]
    root         3     2  0 09:52 ?        00:00:00 [ksoftirqd/0]
    root         4     2  0 09:52 ?        00:00:00 [kworker/0:0]
    root         5     2  0 09:52 ?        00:00:00 [kworker/0:0H]

      ps ax(BSD风格)输出的第一列是ProcessID即PID,进程号。第三列是进程当前状态(S代表睡眠,SW代表在睡眠和等待,R代表在运行中)

    mesogene@mesogene-team:~$ ps ax  
      PID TTY      STAT   TIME COMMAND
        1 ?        Ss     0:01 /sbin/init
        2 ?        S      0:00 [kthreadd]
        3 ?        S      0:00 [ksoftirqd/0]
        4 ?        S      0:00 [kworker/0:0]
        5 ?        S<     0:00 [kworker/0:0H]
        7 ?        S      0:01 [rcu_sched]
        8 ?        S      0:00 [rcuos/0]
        9 ?        S      0:00 [rcuos/1]

    二、kill与killall(结束进程)

    kill使用方法是:kill  [PID],kill只能通过进程号PID结束进程。

    mesogene@mesogene-team:~$ kill 7422

    killall使用方法:killall [ProcessName],killall只能通过进程名来结束进程。killall支持通配符。

    mesogene@mesogene-team:~$ killall http*      #结束所有以http开头的进程。
  • 相关阅读:
    字符编码相关
    函数之形参与实参
    文件操作模式
    函数对象,名称空间,作用域,和闭包
    吴裕雄天生自然SPRINGBOOT开发实战处理'spring.datasource.url' is not specified and no embedded datasource could be autoconfigured
    吴裕雄天生自然SPRINGBOOT开发实战处理XXXX that could not be found.
    吴裕雄天生自然SPRINGBOOT开发实战SpringBoot HTML表单登录
    吴裕雄天生自然SPRINGBOOT开发实战SpringBoot REST示例
    吴裕雄天生自然SpringBoot开发实战学习笔记处理 Could not write metadata for '/Servers'.metadata\.plugins\org.eclipse.core.resources\.projects\Servers\.markers.snap (系统找不到指定的路径。)
    吴裕雄天生自然SPRINGBOOT开发实战SpringBoot Tomcat部署
  • 原文地址:https://www.cnblogs.com/Richard-xie/p/4391756.html
Copyright © 2011-2022 走看看