• Bootcamp (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • Nodejs

    BootcampBackend2020-12-17


    Creating a skeleton website

    [mdn document])https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes)

    npm install -g express

    Reference

    # Run helloworld on Linux/macOS
    DEBUG=helloworld:* npm start

    The DEBUG command creates useful logging, resulting in an output like that shown below.

    5 Performance Tips for Node.js Applications 9 Production Ready Node.js Tips and Tricks Node.js Performance Tip of the Week: Scaling with Proxies and Clusters

    TIPS

    • Node.js is now considered a key tool for all kinds of microservices‑based development and delivery.

    It shares these architectural characteristics with NGINX and solves the C10K problem – supporting more than 10,000 concurrent connections. Node.js is not so great for serving static content – images and JavaScript files, for example – or load balancing across multiple servers.

    Nginx:

    1. Implement a reverse proxy server
    2. Cache static files
    3. Load balance traffic across multiple servers
    4. Proxy WebSocket connections
    5. Implement SSL/TLS and HTTP/2

    “If #nginx isn’t sitting in front of your node server, you’re probably doing it wrong.” Bryan Hughes on Twitter

    • 10 things you should do while running Node.js in production
    • Use reverse proxy
    • Use monitoring tools: Keymetrics – by PM2, Trace – by RisingStack
    • Remove console.log, warning, debug
    • Using external store for global store: redis as a session store.
    • use SSL
    • Recheck security measures
    • Keep real app only accessible by private users
    • Run Node.js in cluster mode
    • Perform more I/O and less CPU intensive task
    • Use process managers: There are npm modules such as forever, pm2 to handle process. We recommend PM2, because of its ease of use and keymetric integration.

    Node

    pub/sub or observer DP ?

    The non-blocking I/O engine of Node.js – libuv -

    The Observer Pattern(EventEmitter) maintains a list of dependents/observers and notifies them

    var events = require('events');
    var eventEmitter = new events.EventEmitter();
    
    var ringBell = function ringBell() {
      console.log('tring tring tring');
    }
    eventEmitter.on('doorOpen', ringBell);
    
    eventEmitter.emit('doorOpen');

    Node.js uses `streams to handle incoming data

    A stream is an abstract interface for working with streaming data in Node.js. The stream module provides a base API that makes it easy to build objects that implement the stream interface.

    Handing POST request like upload file use request.on(‘data, chunk), request.on(‘end’, ()=>cb);

    server-sider rendering

    Server Side Rendering based on routes matched by React-router.

    app.use((req, res) => {
        match({
            routes,
            location: req.url
        }, (err, redirectLocation, renderProps) => {
            if (err) {
                return res.status(500).end('Internal server error');
            }
    
            if (!renderProps) {
                return res.status(404).end('Not found!');
            }
    
            const initialState = {
                posts: [],
                post: {}
            };
    
            const store = configureStore(initialState);
    
            fetchComponentData(store.dispatch, renderProps.components, renderProps.params).then(() => {
                const initialView = renderToString(
                    <Provider store = {store} >
                      <RouterContext {...renderProps}/>
                    </Provider>
                );
    
                const finalState = store.getState();
    
                res.status(200).end(renderFullPage(initialView, finalState));
            }).catch(() => {
                res.end(renderFullPage('Error', {}));
            });
        });
    });

    generic web-server

    • webpack-dev-server
    • serve
    • http-server
    • pm4
    • forever
    • nodemon