iT邦幫忙

4

node.js - express #1

pyk 2017-04-19 20:57:3933370 瀏覽

今天和一些node.js前輩請教node.js要從哪邊開始學習,
因為我想從後端學習,所以被推薦了express,以後如果學得深了就能往Koa學習!

我寫前端約一年,多數是使用jQuery這樣,SQL也會一些,就是不知道怎麼串連啊啊啊啊。

文章只是想紀錄我的學習心得和督促自己/images/emoticon/emoticon18.gif
這些資源都是來自於網路上的,可能有一些個人註解罷了!


Express

就像它的名字一樣,是一個輕量的Node.js web應用程式開發架構框架(其實express是快速的意思xD)
可以很快速的開發Web Application

安裝

先安裝express (要先有npm哦,大家應該都有吧xD)

$ npm install express --save

接下來是安裝重要的module (這段真是有看沒有懂QQ)

  • body-parser 解析json、row、文本、URL-encoded格式的表單資料
    Form表單會用到
  • cookie-parser 解析Cookie頭和填充req.cookies通過cookie名字鍵控對象
  • multer 一個Node.js的中間件處理multipart/form-data
    中間件是指http發出的請求
$ npm install body-parser --save
$ npm install cookie-parser --save
$ npm install multer --save

第一個應用:簡單的Server

var express = require('express'); //call express來用
var app = express();

app.get('/', function (req, res) {
//如果有收到 / 在頁面上回傳'Hello World'
   res.send('Hello World');
});

var server = app.listen(8081, function () {
//開啟listen port在8081,port的概念在網路方面的課程應該會有教到
//簡單說就是開一個洞,去和其他的application連接

  var host = server.address().address;
  var port = server.address().port;

  console.log("Example app listening at http://%s:%s", host, port);

});

雖然javascript可以不需要 ; 但我還是習慣都加啦QQ
接下來就很簡單的執行你的檔案

$ node server.js

會看到

Example app listening at http://0.0.0.0:8081

之後打開瀏覽器輸入網址 http://0.0.0.0:8081 就會看到畫面了

http://0.0.0.0 / http://127.0.0.1 / http://localhost 其實都會導向一樣的地方


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
youngtin
iT邦新手 5 級 ‧ 2017-06-09 17:05:00

本身為node超級新手,
你的文章給我很大的幫助!
感謝分享!

我要留言

立即登入留言