顯示具有 網站開發 標籤的文章。 顯示所有文章
顯示具有 網站開發 標籤的文章。 顯示所有文章

2021年4月11日 星期日

JQuery - $( document ).ready()

JQuery - $( document ).ready()

鑒於 $( document ).ready() 有縮寫,導致初學者如我看不懂,這裡紀錄一下。
  • 基本版本
    $( document ).ready() 基本上是等 Document Object Model ( DOM ) 好了後,可以執行 JavaScript code 時啟動。另外常見的 $( window ).on( "load", function() { ... }) 則是等整個文件都處理完畢後 ( 包含 image, iframe...等等 ) 才執行,而非只有 DOM。
        // A $( document ).ready() block.
        $( document ).ready(function() {
            console.log( "ready!" );
        });
  • 縮寫版本
    老練的開發者會寫 $() 代替 $( document ).ready(),但最好不要。
        // Shorthand for $( document ).ready()
        $(function() {
            console.log( "ready!" );
        });
參考資料 :
1.JQuery_Doc

2021年1月25日 星期一

重新踏入網頁開發 (7) - React

 React - Introduction

    A JavaScript library for building user interfaces. React 在 MVC 分類上屬於 View,也就是主要用來開發前端。比起直接使用 npx create-react-app,我這裡想紀錄一些較簡單且原始的 React Sample Code。
  • 原本既有的程式碼
        <!-- Some HTML -->
        <div id="mydiv"></div>
        <!-- Some HTML -->
  • 導入 React library
        <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
        <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
  • React Code
        <script type="text/javascript">
          class HelloWorld extends React.Component {
            render() {
              return React.createElement('h1', {}, 'Hello React');
            }
          }
          const domContainer = document.querySelector('#mydiv');
          ReactDOM.render(React.createElement(HelloWorld), domContainer);
        </script>
  • Result
        <div id="mydiv">
          <h1>Hello React</h1>
        </div>

 React - JSX ver

    現今 React 大多都會用 JSX 語法,較為便捷直觀。這裡附上上面純 React 跟 React & JSX 的 Sample Code。
  <!DOCTYPE html>
  <html>
    
    <!--這兩個 script 讓我們可以在 JavaScripts 寫 React Code -->
    <script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
    
    <!--這個 script 讓我們可以使用 JSX 和 ES6,即使瀏覽器較舊 -->
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
    
    <body>

      <div id="mydiv"></div>
      <div id="mydiv_JSX_ver"></div>

      <!-- 純 React ( 不用 JSX ) -->
      <script type="text/javascript">
        class HelloWorld extends React.Component {
          render() {
            return React.createElement('h1', {}, 'Hello React');
          }
        }
        const domContainer = document.querySelector('#mydiv');
        ReactDOM.render(React.createElement(HelloWorld), domContainer);
      </script>

      <!-- JSX 版本 -->
      <script type="text/babel">
        class Hello extends React.Component {
          render() {
            return <h1>Hello React with JSX!</h1>
          }
        }
        ReactDOM.render(<Hello />, document.querySelector('#mydiv_JSX_ver'))
      </script>

    </body>

  </html>
參考資料 :

2021年1月21日 星期四

重新踏入網頁開發 (6) - Express - 5

 Express - sendFile()

    用 sendFile 把寫好的 HTML 傳出去,然後準備開始前端的開發。
    import express from 'express'
    import path from 'path';

    // 建立 express 這 module
    var app = express()
    const port = 8888
    const __dirname = path.resolve();

    // This responds a GET request for the homepage
    app.get('/', (req, res) => {
        console.log("Got a GET request for the homepage, from");
        res.sendFile(__dirname + "/hello.html");
    })

    var server = app.listen(port, function () {
    var host = server.address().address
    var port = server.address().port
        console.log("Example app listening at http://%s:%s", host, port)
    })
參考資料 :

2021年1月15日 星期五

重新踏入網頁開發 (6) - Express - 4

 Express - App v.s. Router

    Express 通常都是用 app.Method 來做 request 的 routing。而 Express 4.0 加入的 router 則可視為可以掛載的迷你 app。
    import express from 'express'

    var app = express()
    var router = express.Router() 
    const port = 8888

    // Some router level function
    function logger (req, res, next) {
        console.log('Request URL:', req.url)
        console.log('Request Type:', req.method)
        next()
    }
    function welcome (req, res) {
        res.send("Welcome to the localhost!\n")
    }
    function hello (req, res, next) {
        res.send("Hello " + req.params.userName + "\n")
        next()
    }

    router.use(logger)
    router.get('/', welcome)
    router.get('/user/:userName', hello)

    // load router,就可獲得兩個 page ( '/' 跟 '/user/:userName' )
    // 這裡要注意的是要掛載 router 的 app 要用 app.use() 而不能是 app.Method()
    // 若用 app.Method(),'/user/:userName' 就會被 app.Method() 濾掉
    // 只剩一個 page
    app.use('/', router)
    app.listen(port)
上一篇 :
參考資料 :

2021年1月14日 星期四

重新踏入網頁開發 (6) - Express - 3

 Express - Middleware

    Middleware 是會處理 req, res, next 三個物件並在 routing 時執行之 function,基本上就是 routing 時會處理的 callback function。是 Express 中蠻重要的概念。但用例子來理解比較容易。
    import express from 'express'

    // 建立 express 這 module
    var app = express()
    const port = 8888
    var userName = "Unknown"

    function hello(req, res) {
        res.send("Hello " + userName + "\n")
        res.end()
    }

    // 中間的匿名 function 就是 Middleware
    app.get('/hello/:userName.:userPassword', function (req, res, next){
        userName = req.params.userName
        console.log(userName)
        next()
    },  hello)
    
    app.listen(8888)
    所以 Express 可以用這些 Middleware 來作到流程控制,而非使用傳統的 if else。

 Express - Middleware Using

    Middleware 除了自己塞也可以有 2 種 Express 提供的方式使用。
  • Application-level
        import express from 'express'
    
        // 建立 express 這 module
        var app = express()
        const port = 8888
    
        function reqType (req, res, next){
            console.log('Request Type:', req.method)
            next()
        }
    
        function timer (req, res, next){
            var date = new Date()
            console.log('Time:', date.toString())
            if (req.params.userName == "Admin") {
                next('route') // 只能在 app.Method() 使用,在 use() 裡就會只是普通的 next()
            } else {
                next()
            }
        }
    
        function logger (req, res, next){
            console.log
            (
                'User Date:', '\n',
                'User:', req.params.userName, '\n',
                'Password:', req.params.userPassword, '\n'
            )
            res.end()
        }
    
        function warning(req, res, next) {  
            console.log("Warning: Your village is under attack\n")
            res.end()
        }
    
        // app.use() 在 route 之前,所以會先執行 use 裡的 Middleware function
        // 之後在依照 Request Method 去找相對的 app.Method() 做 Reuest Handle
        app.use('/hello/:userName.:userPassword', reqType)
    
        // 正常來講,若 Request 是 GET 且 URL 符合的話,會依照順序執行 routing function
        // 但 timer 裡有 next('route'),符合條件的話會直接把控制權交給下個 routing function,
        // 忽略它本身後面的 Middleware function,這裡就是 logger。
        app.get('/hello/:userName.:userPassword', timer, logger)
        app.get('/hello/:userName.:userPassword', warning)
        app.listen(8888)
  • Router-level
        使用方法跟 app (express()) 一模一樣,差把 app 改成 router (express.Router())
上一篇 :
下一篇 :
參考資料 :

2021年1月11日 星期一

重新踏入網頁開發 (6) - Express - 2

 Express - Route parameters

    Route parameters 可以用來擷取 URL 上的 value。
  • server.js
        import express from 'express'
    
        // 建立 express 這 module
        var app = express()
        const port = 8888
    
        // 回傳 { "userId": xxx, "bookId": xxx }
        app.get('/users/:userId/books/:bookId', function (req, res) {
            res.send(req.params)
        })
    
        // 可以用 "-" 分割,回傳 { "userId": xxx, "authorId": xxx, "bookId": xxx }
        app.get('/users/:userId-:authorId-:bookId', function (req, res) {
            res.send(req.params)
        })
    
        // 可以用 "." 分割,回傳 { "userId": xxx, "authorId": xxx, "bookId": xxx }
        app.get('/users/:userId.:authorId.:bookId', function (req, res) {
            res.send(req.params)
        })
    
        app.get('/users', function (req, res) {
            res.send("/users\n")
        })
    
        var server = app.listen(port, function () {
            var host = server.address().address
            var port = server.address().port
            console.log("Example app listening at http://%s:%s", host, port)
        })
  • Testing ( curl筆記 )
        $ curl -X GET http://localhost:8888/users/34/books/8989
        {"userId":"34","bookId":"8989"}
    
        $ curl -X GET http://localhost:8888/users/34-789-456
        {"userId":"34","authorId":"789","bookId":"456"}
    
        $ curl -X GET http://localhost:8888/users/34.123.258
        {"userId":"34","authorId":"123","bookId":"258"}
    
        $ curl -X GET http://localhost:8888/users/
        /users

 Express - Route handler

    Route handler 的 callback function 可以使用複數個,只要呼叫 next() 這個 function,便可執行下一個 callback function。
  • server.js
        import express from 'express'
    
        // 建立 express 這 module
        var app = express()
        const port = 8888
    
        // 可以塞複數個 callback, 只要確保你有傳和呼叫 next()
        app.get('/normal', function (req, res, next) {
            console.log('Hello from the first callback function!')
            next()
        }, function (req, res) {
            console.log('Hello from the second callback function!')
            res.send("Hi, this is the second callback function!\n")
            res.end()
        })
    
        // array 版
        function callbackA(req, res, next) {
            console.log('Hello from callbackA function!')
            next()
        }
        function callbackB(req, res, next) {
            console.log('Hello from callbackB function!')
            next()
        }
        function callbackC(req, res) {
            console.log('Hello from callbackC function!')
            res.send("Hi, this is callbackC!\n")
            res.end()
        }
    
        app.get('/array', [callbackA, callbackB, callbackC])
    
    
        var server = app.listen(port, function () {
            var host = server.address().address
            var port = server.address().port
            console.log("Example app listening at http://%s:%s", host, port)
        })
  • Testing - client( curl筆記 )
        $ curl -X GET http://localhost:8888/normal
        Hi, this is the second callback function!
        $ curl -X GET http://localhost:8888/array
        Hi, this is callbackC!
  • Testing - server( curl筆記 )
        $ node Learning/11.Express\&ES6/Route_handler.js 
        Example app listening at http://:::8888
        Hello from the first callback function!
        Hello from the second callback function!
        Hello from callbackA function!
        Hello from callbackB function!
        Hello from callbackC function!
上一篇 :
下一篇 :
參考資料 :

2021年1月7日 星期四

重新踏入網頁開發 (6) - Express - 1

 Express

    Fast, unopinionated, minimalist web framework for Node.js,這是 Express 的自我介紹。這裡的 unopinionated 之於 opinionated 較為信任開發者,所以你可以擁有很多作法去達到相同的目的,例如像 PERL/PHP。而 opinionated 的 software 則會只提供一個方法去達到目的,例如撰寫維基百科。沒寫過維基百科,但很顯然維基百科有他的格式存在,而格式都是維基百科的設計師所規定的。他不會讓你自由地的操作 HTML 把它當成你的部落格在寫,你只能照他的方式去更新內容。
    安裝
    npm install express

 Express - Routing

    之前 Routing 的實作是用原生 module: http、url 和自製的 dict 去實現,現在用 express 來達成。
  • server.js
       import express from 'express'
    
       // 建立 express 這 module
       var app = express()
       const port = 8888
    
       // This responds a GET request for the homepage
       app.get('/', (req, res) => {
          console.log("Got a GET request for the homepage");
          res.send('Hello GET!\n')
       })
    
       // This responds a POST request for the homepage
       app.post('/', function (req, res) {
          console.log("Got a POST request for the homepage");
          res.send('Hello POST\n');
       })
    
       // This responds a GET request for the /list_user page.
       app.get('/list_user', function (req, res) {
          console.log("Got a GET request for /list_user");
          res.send('Page Listing\n');
       })
    
       var server = app.listen(port, function () {
          var host = server.address().address
          var port = server.address().port
          console.log("Example app listening at http://%s:%s", host, port)
       })
  • Testing ( curl筆記 )
        $ curl -X GET http://localhost:8888/
        Hello GET!
        $ curl -X POST http://localhost:8888/
        Hello POST
        $ curl -X GET http://localhost:8888/list_user
        Page Listing
    第一個參數,字串支援 Regular Expression
  • server.js
       // This responds a GET request for ab*cd, abxcd, ab123cd, and so on
       app.get('/ab*cd', function(req, res) {   
          console.log("Got a GET request for /ab*cd");
          res.send('Page Pattern Match\n');
       })
    
       // This responds a GET request for book1, book2 and book3
       app.get('/book[123]', function(req, res) {   
          console.log("Got a GET request for /ab*cd");
          res.send('Page Pattern Match\n');
       })
  • Testing ( curl筆記 )
        $ curl -X GET http://localhost:8888/ab123cd
        Page Pattern Match
    
        $ curl -X GET http://localhost:8888/ab123456cd
        Page Pattern Match
    
        $ curl -X GET http://localhost:8888/book1
        Page Pattern Match
    
        $ curl -X GET http://localhost:8888/book2
        Page Pattern Match
    
        $ curl -X GET http://localhost:8888/book3
        Page Pattern Match
    
        $ curl -X GET http://localhost:8888/book4
        <!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="utf-8">
        <title>Error</title>
        </head>
        <body>
        <pre>Cannot GET /book4</pre>
        </body>
        </html>
上一篇 :
下一篇 :
參考資料 :


LINUX - cURL 筆記

cURL - Client URL

    cURL is a command-line tool for getting or sending data including files using URL syntax。網站在開發 Restful API 時,測試會用到的最基本工具。這邊紀錄一些常用的參數
  • 基本的 request
        # Default 是 GET
        curl http://localhost:8888/
        # 指定 request (--request 可用 -X 代替)
        curl --request GET http://localhost:8888/
        curl --request POST http://localhost:8888/
  • 將 response 存成檔案
        # -o 加檔名, -O 直接將 URL 當檔名 (ex. list_user)
        $ curl -o temp  http://localhost:8888/list_user
        $ curl -O http://localhost:8888/list_user
  • 若 response 301/302(redirect),會跟著 redirect
        # 會 redirect 到 http://www.google.com    
        curl -L http://google.com
  • 把整個 request 流程 trace 儲存到 file
        curl --trace-ascii debugdump.txt -L http://google.com
參考資料 :


2020年11月11日 星期三

重新踏入網頁開發 (5) - Blocking & Non-Blocking

Blocking & Non-Blocking

    Nodejs 為事件觸發的單一執行緒,不像 Apache + PHP 會自動開一個 Thread 去接每一個 Request。
    Blocking Code 示範 (程式碼另有 index.js 請參照重新踏入網頁開發 (4) - Dependency injection)
  • route.js
        function start_requset_handle () {
            console.log("Start request.");
            
            // Sleep for 10 seconds
            function sleep(milliSeconds) {
                var startTime = new Date().getTime();
                while (new Date().getTime() < startTime + milliSeconds);
            }
            sleep(10000);
        }
    
        function upload_requset_handle () {
            console.log("Upload request.");
        }
    
        function requset_handler (url) {
            var handle = {};
            handle['/'] = start_requset_handle;
            handle['/start'] = start_requset_handle;
            handle['/upload'] = upload_requset_handle;
    
            if (typeof handle[url] === 'function') {
                handle[url]();
            } else {
                console.log("No request handler found for " + url);
            }
        }
    
        export function route(url) {
            requset_handler(url);
        }
  • server.js
        import http from 'http'
        import url from 'url'
    
        export function serverStart(route) {
            function onRequest(request, response) {
                var path_name = url.parse(request.url).pathname;
                console.log("Request for " + path_name + " received");
                
                // Routing function
                route(path_name);
    
                response.writeHead(200, {"Content-Type": "text/plain"});
                response.write("Hello World.\n");
                response.end();
            }
            http.createServer(onRequest).listen(8888);
            console.log("Server has started...");
        }
    這裡先用 requset_handler() 這 function 去應對 2 種 url 的 request。client 端 request http://localhost:8888/start 會等 10 秒才回覆 Hello World.,request http://localhost:8888/upload 則立即回覆。因為 nodejs 為單一執行緒,如果有人先 request start, 後面即使是 request upload 也要跟著等 10 秒。
    Non-Blocking Code 示範 (程式碼另有 index.js 請參照重新踏入網頁開發 (4) - Dependency injection)
  • route.js
        import { exec } from 'child_process';
    
        function start_requset_handle (response) {
            console.log("Start request.");
            var content = "empty\n";
            exec("ls -alh", function (error, stdout, stderr) {
                content = stdout;
                response.writeHead(200, {"Content-Type": "text/plain"});
                response.write(content);
                response.end();
            });
            // 放在外面會讓 server 直接回 empty
            // response.writeHead(200, {"Content-Type": "text/plain"});
            // response.write(content);
            // response.end();
        }
    
        function upload_requset_handle (response) {
            console.log("Upload request.");
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write("Uploading...\n");
            response.end();
        }
    
        function requset_handler (url, response) {
            var handle = {};
            handle['/'] = start_requset_handle;
            handle['/start'] = start_requset_handle;
            handle['/upload'] = upload_requset_handle;
    
            if (typeof handle[url] === 'function') {
                handle[url](response);
            } else {
                console.log("No request handler found for " + url);
            }
        }
    
        export function route(url, response) {
            requset_handler(url, response);
        }
  • server.js
        import http from 'http'
        import url from 'url'
    
        export function serverStart(route) {
            function onRequest(request, response) {
                var path_name = url.parse(request.url).pathname;
                console.log("Request for " + path_name + " received");
                
                // Routing function
                route(path_name, response);
            }
            http.createServer(onRequest).listen(8888);
            console.log("Server has started...");
        }
    import nodejs 內建的 child_process,解決了問題。傳 response 進去是為了讓 request 處理完再做回覆。
上一篇 :
下一篇 :
參考資料 :

2020年10月30日 星期五

重新踏入網頁開發 (4) - Dependency injection

耦合性 (Coupling,dependency)

    耦合性是指一程式中,模組及模組之間資訊或參數依賴的程度。低耦合性是結構良好程式的特性,低耦合性程式的可讀性及可維護性會比較好。舉例來說 Module A 使用了 Module B 功能, 今天 Module B 更改了功能而造成 Module A 必須也更改使用其功能的邏輯, 則會說 Module A 對 Module B 依賴程度高, 具有高耦合性。所以你要維護 B 就要一起維護 A, 邏輯參在一起想必 module 的可重複使用率也低。

解耦 > Dependency injection

    高耦合 > 解耦 > 低耦合。解耦方法很多這裡使用的是 Dependency injection (依賴注入>///<)。拿前幾些章節裡的 Server 跟 Route 舉例。
    沒有依賴注入的 Server + Route
  • route.js
        export function regular_route(pathname) {
            console.log("Start routing path name \"" + pathname + "\"");
        }
  • Server.js
        import http from 'http'
        import url from 'url'
        import { regular_route } from './route.js';
    
        export function serverStart() {
            function onRequest(request, response) {
                var path_name = url.parse(request.url).pathname;
                console.log("Request for " + path_name + " received");
                
                // my Route function
                regular_route(path_name);
    
                response.writeHead(200, {"Content-Type": "text/plain"});
                response.write("Hello World.\n");
                response.end();
            }
            http.createServer(onRequest).listen(8888);
            console.log("Server has started...");
        }
  • index.js
        import {serverStart} from './Server.js'
        serverStart();
    老實說看起來像最佳解, 但當你要換另一種 routing function, 你正常來說要更改 2 個地方。import 的部份可能要改, 因為是新的檔案或其他大 module 裡的 route function。function name 可能要改, 因為新的 routing function 不叫 regular_route。驗證了上面所說的你改 B 結果 A 也要跟著改,量子糾纏...阿不對是"高耦合"。才 2 個看起來還好,但隨著專案擴的越大,你改 routing function 的時間成本也越大,甚至改天不用 route 用其它東西去處理 URL,那就更慘了。
    依賴注入的 Server + Route
    這邊切入的點是 Server 這 module 主要不變工作應該只有接收與回覆,中間的 routing function 是會依照時空背景不同去更動(沒錯,就是這麼彈性且有價值,誰說整天看政治新聞沒用的),可以被抽象化成一個動作,而這個動作可以當作參數傳進 Server 這 module。(說穿了就是做 callback function)
  • route.js (沒變)
        export function regular_route(pathname) {
            console.log("Start routing path name \"" + pathname + "\"");
        }
  • Server.js
        import http from 'http'
        import url from 'url'
    
        export function serverStart(route) {
            function onRequest(request, response) {
                var path_name = url.parse(request.url).pathname;
                console.log("Request for " + path_name + " received");
                
                // my Route function
                route(path_name);
    
                response.writeHead(200, {"Content-Type": "text/plain"});
                response.write("Hello World.\n");
                response.end();
            }
            http.createServer(onRequest).listen(8888);
            console.log("Server has started...");
        }
  • index.js
        import { serverStart } from './Server.js'
        import { regular_route } from './route.js';
        serverStart(regular_route);
    這樣你改 routing function 就不會需要動到 Server.js (改成動index.js...),甚至改傳吃豬肉這 function,Server 也照執行沒什麼問題。
上一篇 :
下一篇 :
參考資料 :

2020年10月29日 星期四

重新踏入網頁開發 (3) - Route

Route ( URL )

    通過不同的 URL 去區別不同請求
  • Server.js
        import http from 'http'
        import url from 'url'
    
        function onRequest(request, response) {
            // 會紀錄 request url
            var path_name = url.parse(request.url).pathname;
            console.log("Request for " + path_name + " received");
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write("Hello World.\n");
            response.end();
        }
    
        http.createServer(onRequest).listen(8888);
        console.log("Server has started...");
  • 用 curl 去 request
        $ curl http://localhost:8888/
        Hello World.
        $ curl http://localhost:8888/students/1
        Hello World.
  • 伺服器端顯示
        $ node Server.js 
        Server has started...
        Request for / received
        Request for /students/1 received
上一篇 :
下一篇 :
參考資料 :

2020年10月28日 星期三

Node.js - 支援 ES6 module

Nodejs 支援 ES6 module

    要讓 nodejs 去支援 ES6 module,必須去改寫 package.json。
    開啟一個新的 nodejs project
  • 首先是 node package manager
        npm init
        # 應該會有一堆要填的,若沒任何想法可以改下 npm init -y 自動幫你填完
  • 這時候多一個 package.json 的檔案,裡面有你剛填的資料
        {
          "name": "react",
          "version": "1.0.0",
          "description": "",
          "main": "index.js",
          "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1"
          },
          "repository": {
            "type": "git",
            "url": "git+https://github.com/JunYe1993/React.git"
          },
          "author": "JunYe1993",
          "license": "ISC",
          "bugs": {
            "url": "https://github.com/JunYe1993/React/issues"
          },
          "homepage": "https://github.com/JunYe1993/React#readme"
        }
  • package.json 裡多加一個設定
        ...
          "type": "module"
        }
    若 node 版本低於 v13, 會噴 Warning。
  • node 版本低於 v13
        $ node HelloWorld.js 
        (node:29009) ExperimentalWarning: The ESM module loader is 
        experimental.
        Hello World
參考資料 :

2020年10月27日 星期二

重新踏入網頁開發 (2) - ES6

模組化

    Ryan Dahl 創造的 node.js 專案之所以如此重要, 是因為其理念為 模組(module), 讓 javascript 的大規模專案得以實現, 也讓 javascript 可以做伺服器端程式設計。

node.js 的模組

    這裡示範 node.js 原生的模組,
  • moduleA.js
        // 想像這裡有隱藏程式碼
        // # var module = new Module(...);
        // # var exports = module.exports;
        // index.js 在 call "require("./ModuleA");"
        // 等於回傳 module.exports
        var name = "JunYe";
        exports.name = name;
  • moduleB.js
        // 想像這裡有隱藏程式碼
        // # var module = new Module(...);
        // # var exports = module.exports;
        // index.js 在 call "require("./ModuleB");"
        // 等於回傳 module.exports
        var name = "Daniel";
        exports.name = name;
  • index.js
        var a = require("./moduleA");
        var b = require("./moduleB");
        console.log(a.name + " and " + b.name + " both are the best.");
  • 執行 node index.js
        $>:node index.js 
        JunYe and Daniel both are the best.

ES6 的模組

    ES6 是較新的 javascript 規範, 沒錯就是規範, javascript 並非正規的程式語言, 而是一種語言規範, 而其 runtime 的執行則讓各個瀏覽器引擎決定(nodejs也是其中之一)。雖然 ES6 是較新的規範, 但大部分的瀏覽器都有支援了。以下為 nodejs 支援 ES6 的示範
  • studentA.js
        export var _name = "JunYe";
        export var _class = "SSS";
        export var _number = "3064";
  • function.js
        export function intro (name, number) {
            console.log("My name is " + name + ", and my number is " + number);
        };
  • index.js
        // 要在 package.json 加 type: module
        // 才能達到 <script src="./function.js" type="module"></script> 的效果
        // 這邊不知道為什麼要加 .js 才給過
        import { intro } from "./function.js";
        import { _name, _number } from "./studentA.js";
        intro(_name, _number);
  • 執行 node index.js
        $>:node index.js 
        My name is JunYe, and my number is 3064
上一篇 :
下一篇 :
參考資料 :

2020年10月10日 星期六

重新踏入網頁開發 (1) - Nodejs

前言

    網頁開發是一個對我而言還蠻陌生的東西, 因為我都是自學所以總是得過且過。自學通常就會拿手邊資源也就是公司內部系統的 Code。公司的 Code 較為古早, 不外乎 3 個基本的元素, http server(apache), PHP, 前端(html, css, javascript)。 此時的我靠著 w3schools 就能完成所有事情。既然重新踏入開發網頁, 就去學最新的東西 (目標是 react), 即使路途遙遠, 但相對的有很多東西可以紀錄。

Node.js

    離開了較為古老的開發方法後, 進入到所謂較為現代網站開發, 查了查似乎都繞不開 node.js。所以我想在這個下點功夫, 學學這個可以在後端執行的 javascript。

    學程式學語言的第一步通常是 Hello World,所以那就來吧。(當然要先裝 Node.js)
  • 創一個檔案 HelloWorld.js
        console.log("Hello World");
  • 在 CMD 執行 HelloWorld.js
        $>node HelloWorld.js
        Hello World
    再一個貨真價實的後端 Hello World
  • 創一個檔案 Server.js
        var http = require("http");
    
        http.createServer(function (request, response) {
            response.writeHead(200, { "Content-Type": "text/plain" });
            response.write("Hello World");
            response.end();
        }).listen(8888);
  • 在 CMD 執行 Server.js (如果你是 windows 請按允許存取)
        $>node Server.js
    
    
  • 開啟瀏覽器, 讀取 http://localhost:8888/ (chrome 書籤太多...科)

結語

    老實說上面的程式碼對我來說還蠻震驚的, 因為它取代了 apache (http server), 一個我從來懶得去研究的東西。看來路途比想像的遙遠
下一篇 :
參考資料 :

2019年11月26日 星期二

MySQL 筆數查詢、分頁查詢

N 筆資料查詢

    SELECT * FROM 成績單 LIMIT 3    // 取成績單前三筆資料
    SELECT * FROM 成績單 ORDER BY 分數 DESC LIMIT 3 // 取分數前三名的資料
    SELECT * FROM 成績單 ORDER BY 分數 DESC LIMIT 3, 7 // 取第四名到第十名的資料

分頁查詢

     隨著查詢筆數越來越多,網站可能就會需要用分頁來表示查詢結果。
     這裡會出現一個問題 Offset ( LIMIT 的第1個參數 ) 越大,查詢速度越慢。
    SELECT * FROM 成績單 LIMIT  100000, 1         // 大 Offset
     Google 的結果是 InnoDB 會有的情況,InnoDB 會掃 100001 筆資料,然會再把 100000 筆資料給捨棄,導致查詢速度越來越慢。所以解決方法為先只掃索引
    SELECT * FROM 成績單 WHERE id >= (SELECT id FROM 成績單 LIMIT 100000, 1) LIMIT 1

2019年11月5日 星期二

2019年10月31日 星期四

CSS Selector Priority

CSS

在套用的時候,是有優先順序的。

沒研究前我以為是程式碼先後或最靠近的element
<table id='table'>
 <tr>
  <td><table class='subtable'>...</table></td>
  <td><table class='subtable'>...</table></td>
  <td><table class='subtable'>...</table></td>
 </tr>
</table>
我在將上面的 HTML 套用 CSS
#table {
 XXX
}
.subtable {
 YYY
}
所有的 table 都照著 XXX 顯示,所以在設計時,要盡量避免 id selector 的使用當你的 element 還包含很多 elements 的時候。以下為 CSS Selector 的 Priority

參考資料 : http://qnimate.com/dive-into-css-specificity/Colum

2019年10月17日 星期四

JQuery 自動將 Listener 作用於動態新增的 Element

Adding event listeners to dynamically added elements

拿 click 舉例
$(".myclass").click(function(){
     // do something
});
若在整個網頁讀取完後,才新增 class = 'myclass' 的 element 不會有這個 Event Listener

這時要用 on
$('#mydiv').on('click', '.myclass', function(){
     // do something
});
onJquery 1.7 才出現

這裡提供 Jquery > 1.4.2 的方法
$('#mydiv').delegate('.myclass', 'click', function(){
     // do something
});

參考資料 :
1. Adding event listeners to dynamically added elements
2. https://stackoverflow.com/questions/21625231/on-is-not-a-function-jquery-error

2019年7月23日 星期二

MySQL 備份

備份 MySQL


備份單一資料庫
mysqldump -u root -p database_name > backup.sql
備份多個資料庫
mysqldump -u root -p --databases db1 db2 > backup.sql
備份全部資料庫
mysqldump -u root -p --all-databases > backup.sql
備份單一資料表
mysqldump -u root -p database_name table_name > backup.sql
備份多個資料表
mysqldump -u root -p database_name table1 table2 > backup.sql
備份資料庫除了特定資料表
mysqldump -u root -p database --ignore-table=database.table1 > database.sql


導入 MySQL


導入單一資料庫
mysql -u root -p database_name < backup.sql
導入多個資料庫
mysql -u root -p < backup.sql

Popular Posts