Sails第一课 Sails的HelloWorld!

我是怎么也没有想到,一个写跑马灯的javascript现在成了所有编程语言里唯一的full-stack语言,而这一切需要感谢两个组织机构的大力支持,一个是ECMAScript技术标准,它统一标准,让各大浏览器对javascript全面支持;另一方面,就是Chrome的V8引擎,大大提升了javascript的性能。

top10-computer-language
2018是javascript上升最快的一年
  • 闲话少说,先用Sails来一个Hello,World! 这创建一个新的项目,选择2,创一个空的项目。 

 

wenewzha:tmp wenewzhang$ sails new helloworld
 Choose a template for your new Sails app:
 1. Web App  ·  Extensible project with auth, login, & password recovery
 2. Empty    ·  An empty Sails app, yours to configure
 (type "?" for help, or <CTRL+C> to cancel)
? 2
 info: Installing dependencies...
Press CTRL+C to cancel.
(to skip this step in the future, use --fast)
 info: Created a new Sails app `helloworld`!
  • 创建一个Controller,这时自动在api/controllers目录下,生成一个HelloworldController.js文件,
wenewzha:hello wenewzhang$ cd ../helloworld/
wenewzha:helloworld wenewzhang$ sails generate controller helloworld getMessage
 info: Created a new controller ("helloworld") at api/controllers/HelloworldController.js!
wenewzha:helloworld wenewzhang$

代码如下:

/**
 * HelloworldController
 *
 * @description :: Server-side actions for handling incoming requests.
 * @help        :: See https://sailsjs.com/docs/concepts/actions
 */

module.exports = {
  

  /**
   * `HelloworldController.getMessage()`
   */
  getMessage: async function (req, res) {
    return res.json({
      todo: 'getMessage() is not implemented yet!'
    });
  }

};

  • 修改HelloworldController.js文件,将getMessage修改成如下内容:
  getMessage: async function(req, res) {
    sails.log.info("sails log say: Hello World !");
    console.log("console say: Hello World !"); // To see result on console
    return res.send("res say: Hello World !");
  }
  • 修改config/router.js文件,增加一行:
  '/helloworld': 'HelloworldController.getMessage'
  • 终端里再执行:sails inspect:
wenewzha:helloworld wenewzhang$ sails inspect

 info: Running app in inspect mode...
 info: ( to exit, type <CTRL>+<C> )

Debugger listening on ws://127.0.0.1:9229/c8f655d0-1add-4d4b-9156-9f94b221a36c
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

 info: Starting app...

 info:
 info:                .-..-.
 info:
 info:    Sails              <|    .-..-.
 info:    v1.1.0              |\
 info:                       /|.\
 info:                      / || \
 info:                    ,'  |'  \
 info:                 .-'.-==|/_--'
 info:                 `--'-------'
 info:    __---___--___---___--___---___--___
 info:  ____---___--___---___--___---___--___-__
 info:
 info: Server lifted in `/Users/wenewzhang/Documents/sl/tmp/helloworld`
 info: To shut down Sails, press <CTRL> + C at any time.
 info: Read more at https://sailsjs.com/support.

debug: -------------------------------------------------------
debug: :: Wed Dec 26 2018 10:32:56 GMT+0800 (China Standard Time)

debug: Environment : development
debug: Port        : 1337
debug: -------------------------------------------------------
 info: sails log say: Hello World !
console say: Hello World !
Debugger attached.

在浏览器里打开http://localhost:1337/helloworld,在Chrome里打开chrome://inspect/#devices

一个带有debug的helloworld就完成了!

Tags

添加新评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。