Express hello world

来源:互联网 发布:mmd动作数据怎么用 编辑:程序博客网 时间:2024/06/06 00:21

http://expressjs.com/en/starter/hello-world.html

一、安装node.js ,Express 之后, 建立一个index.js ,下面是服务器端的代码:

const express = require('express')

const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

 1、 app.get('/', function (req, res) {
  res.send('Hello World!')
})
      回调函数返回结果。

      /:表示浏览器上的url地址是根目录,服务器只响应/目录的请求,其它则是404错误。

    app.get():就是服务器传回一客户需要的信息。

  2、表示服务器在3000口监听,有浏览器请求,就调用app.get传回数据给浏览器。

   app.listen(3000, function () {
  console.log('Example app listening on port 3000!')

二、执行

     D:\setupsoft\nodes>node  newapp/index.js
              Example app listening on port 3000!

三、在浏览器输入:http://localhost:3000/