concept server in category node

This is an excerpt from Manning's book Next.js in Action.
Our users will interact with a production build of our React application, served by a production server. The production build is different from the builds that are served to the browser by the dev server. For example, in production we don’t need to listen for new bundles being sent from the server. The bundles are compressed because the time taken to deliver the application to the user matters in this scenario, unlike during development. The compiler used to produce the production bundle might perform several other optimizations, like ensuring that any code that isn’t used by the app isn’t included in the build.
Static websites that predate the modern web were comprised mainly of HTML files and other resources that formed parts of those pages, like images and stylesheets. The browser would request a URL and the server would return content that was ready to display. Today, much of the content that we see on the web is generated dynamically. For example, React applications are made up of components that are implemented via JavaScript. Before any content can be displayed, a bunch of JavaScript needs to run. Often, data needs to be fetched from an API endpoint before any content is visible to the user. While we can do a lot more with modern technology, it comes at a cost. Static content that comes from the server, ready to display, means less wait time for the user and an overall better experience.
React components can be rendered on the server, resulting in static page content that can be sent directly to the browser. If you’ve ever used a dynamic server-side language before, like ASP.NET or PHP, server-side React is the same idea. The browser hits some URL in your application, and the code that lives at the URL generates static content. This content is then displayed by the browser as soon as it arrives – there’s no need to wait for JavaScript code to run in the browser to generate it.
Server-side React is rarely used on its own. Instead, React applications rely on components being rendered on the server and in the browser. Here’s an illustration that shows difference the two approaches:
Figure 1.2 Server-side rendering versus client-side rendering
![]()
The left side shows what server-side rendering looks like. The React component is rendered by the server, resulting in static content. The content is then sent to the browser and displayed. The time between the response arriving at the client and the user seeing the component displayed on their screen is very short.
The right side of the illustration shows what browser rendering looks like for React components. The component is served to the client by the server as part of the JavaScript bundle for the application. Once the browser receives the code bundle, there is still no content visible to the user. This is because the browser needs to render the components, meaning that the time between everything arriving from the server and the user seeing something on their screen is longer.
React applications use both approaches. If we were to rely only on server-side rendering, we would end up putting unnecessary load on the server and transferring a lot of data over the network. If we only use browser rendering, then the user will have to wait longer before they’re able to see anything on their screen. Instead, we can render the content for the user on the sever when they first load the application so that they can see application content as soon as everything arrives from the server. Then, as the user interacts with the application, React components are rendered by the browser since all the application code is loaded and ready.
Listing 5.23: Styles rendered by the server and sent to the browser on initial page load
<style id="__jsx-73122009"> button.jsx-73122009 { margin: 3px; padding: 5px; border-style: none; } </style> <style id="__jsx-2949259094"> h2.jsx-2949259094 { margin-top: 5px; margin-bottom: 5px; } section.jsx-2949259094 { width: 300px; border: solid 1px; margin: 5px; padding: 3px; } </style>

This is an excerpt from Manning's book Get Programming with Node.js.
In the following figure, the client represents the browser on which a user may view your application. The server is where your application runs and handles any data submitted by the user. Also, the server often renders the user interface in many cases in which the client expects one.
You’ve built Node.js web applications that feature client-to-server communication. When the client wants to view a web page or post data, your application generates an HTTP request to the server. This method of communication over the internet has been around for a long time, celebrating its 20th birthday in 2017. In technology years, that’s old. Although developers still heavily rely on the request-response cycle, it isn’t the most effective method of communication for every use case.
What if you want to view the scores of an NBA basketball game in real time, for example? You can load the page containing scores and statistics, but you’d need to reload the page every time you wanted to see an update in information. For a basketball game, these changes can come as rapidly as every second. Repeatedly creating GET requests to the server is a lot of work to expect from the client. Polling is used to generate repeated requests from the client to the server in anticipation of updated server data. Polling uses the standard techniques you’ve used so far to transfer data between the client and server, but it sends requests so frequently that it creates an illusion of an open channel of communication between both participants (figure 30.1).
To further improve on this technique, long polling was developed to reduce the number of requests needed to get updated data. Long polling behaves similarly to polling, in that the client makes repeated requests to the server for updated data, but fewer requests are made. Instead of making hundreds of requests when only dozens of them receive updated data, long polling allows requests to stay open as long as HTTP allows before the requests time out. Within that time—say, 10 seconds—the server can hold on to the request and either respond with updated data when the server receives it or respond with no changes before the request times out. This more-efficient approach has allowed web browsers and devices to experience a sense of real-time informational exchange over a protocol that hasn’t changed much for decades.
Although these two methods are widely used, a recent new addition has allowed platforms like Node.js to thrive. Web sockets were introduced in 2011 to allow an open stream of communication between clients and server, creating a true open channel that allows information to flow in either direction as long as the server or clients are available. Web sockets use a different internet protocol from HTTP but are supported in use with a normal HTTP server. In most cases, a server running with web sockets enabled allows its open channels to be reached over the same application ports you’d use for a typical request-response exchange (figure 30.2).
Now that you have a chat page, you need the guts to get it working. With socket.io installed, you need to require it into your project. Because you want your socket server to run on your existing Express.js HTTP server, require socket.io, and pass it to your Express.js server. Add the require line to main.js below the line where you tell your app to listen on a specified port, as shown in listing 30.3. In this code, you save the running server instance into a constant server so that you can pass the same Express.js HTTP server to socket.io. This process allows socket.io (which I’ll refer to as io) to attach to your application server.

This is an excerpt from Manning's book Getting MEAN with Mongo, Express, Angular, and Node.js 2ED.
Continuing with the trend toward frameworks, the past few years have seen an increasing effort to move the application logic away from the server and into the front end. Think of this as coding the back end in the front end. Some of the most popular JavaScript frameworks doing this are Angular, React, and Vue.js.
Tightly coupling the application code to the front end this way tends to blur the lines between traditional front-end and back-end developers. One of the reasons why people like to use this approach is that it reduces the load on the servers, thus reducing cost. What you’re doing in effect is crowdsourcing the computational power required for the application by pushing that load into users’ browsers.
In a traditional setup, the application code sits and runs on the server. To log in, a user enters their username and password in a form that gets posted to the server.
Then the server checks against a database to validate the login details. Assuming that the login is okay, the server sets a flag or session parameter in the user’s session on the server to declare that the user is logged in.
The server may or may not set a cookie on a user’s browser with the session information. This is common but isn’t technically required to manage the authenticated session; it’s the server that maintains the vital session information. This flow is illustrated in figure 11.2.
Figure 11.2. In a traditional server application, the server validates user credentials stored in the database and adds them to user’s session on the server.
![]()
After this initial handshake and with an established session, when a user requests a secure resource or tries to submit some data to the database, the server validates their session and decides whether they can continue. Figure 11.3 shows how a traditional server setup manages access to secured resources by validating the user session, returning the requested resource when authorization status has been determined.
This is where the change from the traditional approach is obvious. The server is unaware that users are doing anything until they need to access the API and database.
Managing authentication on the server with Passport

This is an excerpt from Manning's book Serverless Applications with Node.js.
Serverless is a method of deploying and running applications on cloud infrastructure, on a pay-per-use basis and without renting or buying servers. Instead of you, the serverless platform provider is responsible for capacity planning, scaling, balancing, and monitoring; the provider is also able to treat your applications as functions.
Listing 2.10
curlcommand for testingPOST /ordersroute (error response)curl -i \ #1 -H "Content-Type: application/json" \ #2 -X POST \ #3 -d '{}' https://whpcvzntil.execute-api.eu-central-1.amazonaws.com/chapter2_3/orders #4 #1 Tell the curl command to print the response with HTTP headers. #2 Set the header to tell the server that you want to send the request parameters in the JSON format. #3 Set the POST method. #4 Send an empty object to your API URL with the appended /orders path.

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.
Managing performance on the server
Listing 2.2. Running the server with Babel—src/server.js
require('babel-register'); #1 require('./app.es6'); #2 #1 Include Babel—it will parse all code that comes after it (not recommended for production use). #2 Include root application code for server. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":36},{\"line\":0,\"ch\":38}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":1,\"ch\":36},{\"line\":1,\"ch\":38}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}]]"} !@%STYLE%@!
This chapter is all about the code that needs to happen on the server. I’ll cover server-specific topics including using Express and using your component and routing code on the server. For example, you’ll learn how to declare the actions of your application in a way that allows the server to fetch them automatically on every page render.
Yes, you read that correctly: you’re going to run your React, React Router, and Redux code on the server. React and React Router each provides server-specific APIs to make this work. Redux needs only minor changes—mostly you’ll call actions from the server. Figure 7.1 illustrates how this works.

This is an excerpt from Manning's book Node.js in Action, Second Edition.
Now if the signature or message were modified, the server would be able to tell. In addition to signed cookies, this module supports JSON-encoded cookies. The next section shows how these work.
$ redis-server [11790] 16 Oct 16:11:54 * Server started, Redis version 2.0.4 [11790] 16 Oct 16:11:54 * DB loaded from disk: 0 seconds [11790] 16 Oct 16:11:54 * The server is now ready to accept connections on port 6379 [11790] 16 Oct 16:11:55 - DB 0: 522 keys (0 volatile) in 1536 slots HT.

This is an excerpt from Manning's book Express in Action: Writing, building, and testing Node.js applications.
node-canvas ports the HTML5 Canvas API to Node, allowing you to draw graphics on the server. See the documentation at https://github.com/Automattic/node-canvas.
In 2009, Node.js came along. Node.js took V8, Google Chrome’s powerful JavaScript engine, out of the browser and enabled it to run on servers. In the browser, developers had no choice but to pick JavaScript. In addition to Ruby, Python, C#, Java, and other languages, developers could now choose JavaScript when developing server-side applications.
JavaScript might not be the perfect language for everyone, but Node.js has real benefits. For one, the V8 JavaScript engine is fast, and Node.js encourages an asynchronous coding style, making for faster code while avoiding multithreaded nightmares. JavaScript also had a bevy of useful libraries because of its popularity. But the biggest benefit of Node.js is the ability to share code between browser and server. Developers don’t have to do any kind of context switch when going from client and server. Now they can use the same code and the same coding paradigms between two JavaScript runtimes: the browser and the server.

This is an excerpt from Manning's book hapi.js in Action.
A server is an object in hapi that can receive and route requests. Before it can accept new requests it must be connected to at least one network socket using a connection.
Debug: my-log-tag Wohoo, the server has started! Debug: my-request-tag Got a request

This is an excerpt from Manning's book Node.js in Practice.
In fact, Node’s strongest and most powerful feature is its standard library. This is really two parts: a set of binary libraries and the core modules. The binary libraries include libuv, which provides a fast run loop and non-blocking I/O for networking and the file system. It also has an HTTP library, so you can be sure your HTTP clients and servers are fast.
This type of encryption is called public key cryptography. Both clients and servers must have private keys. The server can then make its public key available so clients can encrypt messages. To decrypt these messages, access to the server’s private key is required.
Node supports TLS by allowing TCP servers to be created that support several ciphers. The TCP server itself inherits from net.Server—once you’ve got your head around TCP clients and servers in Node, encrypted connections are just an extension of these principles.
Creating a client socket is the same as servers—use dgram.createSocket. Sending a datagram requires a buffer for the payload, an offset to indicate where in the buffer the message starts, the message length, the server port, the remote IP, and an optional callback that will be triggered when the message has been sent:
var message = 'Sample message'; socket.send(new Buffer(message), 0, message.length, port, remoteIP);Listing 7.4 combines a client and a server into a single program. To run it, you must issue two commands: node udp-client-server.js server to run the server, and then node udp-client-server.js client remoteIP to start a client. The remoteIP option can be omitted if you run both locally; we designed this example to be a single file so you can easily copy it to another computer to test sending things over the internet or a local network.
![]()
When you run this example, it starts by checking the command-line options to see if the client or server is required
. It also accepts an optional argument for clients so you can connect to remote servers
.
If the client was specified, then a new client will be created by making a new datagram socket
. This involves using a read stream from the fs module so we have some data to send to the server
—we’ve used __filename to make it read the current file, but you could make it send any file.
Before sending any data, we need to make sure the file has been opened and is ready for reading, so the readable event is subscribed to
. The callback for this event executes the sendData function. This will be called repeatedly for each chunk of the file—files are read in small chunks at a time using inStream.read
, because UDP packets can be silently dropped if they’re too large. The socket.send method is used to push the data to the server
. The message object returned when reading the file is an instance of Buffer, and it can be passed straight to socket.send.
Listing 7.2 shows how to create client connections to an in-process server, and then run assertions on the data sent over the network by the server. Of course, technically this isn’t running over a real network because it all happens in the same process, but it could easily be adapted to work that way; just copy the program to a server and specify its IP address or hostname in the client.
This example highlights two important things: clients and servers can be run together in-process, and Node TCP clients and servers are easy to unit test. If the server in this example were a remote service that we had no control over, then we could create a “mock” server for the express purpose of testing our client code. This forms the basis of how most developers write tests for web applications written with Node.
. It also accepts an optional argument for clients so you can connect to remote servers
.
. This involves using a read stream from the fs module so we have some data to send to the server
—we’ve used __filename to make it read the current file, but you could make it send any file.
. The callback for this event executes the sendData function. This will be called repeatedly for each chunk of the file—files are read in small chunks at a time using inStream.read
, because UDP packets can be silently dropped if they’re too large. The socket.send method is used to push the data to the server
. The message object returned when reading the file is an instance of Buffer, and it can be passed straight to socket.send.