You are previewing Node.js By Example.
O'Reilly logo
Node.js By Example

Book Description

Learn to use Node.js by creating a fully functional social network

In Detail

Node.js is a JavaScript-driven technology, which means that developers can use the same language to write backend code. Its growing community and the large amount of available modules make Node.js one of the most attractive development environments.

Node.js By Example covers Node.js fundamentals and teaches you how to use its technology to architect a project. It shows you how to manage an application's assets and how to implement the Model-View-Controller pattern. The next few chapters demonstrate how to develop the features of your social network. It also empowers you to communicate with a database, including storing, retrieving, and deleting data. The book ends by discussing the most popular ways to test your user interface in an efficient way.

What You Will Learn

  • Get to know the fundamentals of Node.js

  • Understand why architecting is important and what the planning of a typical Node.js application looks like

  • Successfully manage every web application asset such as CSS, JavaScript, or image files

  • Implement the Model-View-Controller pattern in the context of a Node.js application

  • Communicate with a database, including storing, retrieving, and deleting data

  • Implement more complex features such as creating social network pages, tagging, sharing, and liking posts

  • Create a real-time chat capability for users of the social network

  • Explore how to test the user interface of your web application

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Node.js By Example
      1. Table of Contents
      2. Node.js By Example
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Node.js Fundamentals
        1. Understanding the Node.js architecture
        2. Installing Node.js
          1. Running Node.js server
        3. Defining and using modules
        4. Managing and distributing packages
          1. Creating a module
          2. Using modules
          3. Updating our module
        5. Introducing built-in modules
          1. Creating a server with the HTTP module
          2. Reading and writing to files
          3. Working with events
          4. Managing child processes
        6. Summary
      9. 2. Architecting the Project
        1. Introducing the basic layers of the application
        2. The task runner and building system
          1. Introducing Grunt
        3. Discovering Gulp
        4. Test-driven development
        5. The Model-View-Controller pattern
        6. Introducing the REST API concept
        7. Summary
      10. 3. Managing Assets
        1. Serving files with Node.js
        2. CSS preprocessing
        3. Packing client-side JavaScript
          1. Concatenating with Gulp
          2. Modularity in the browser with RequireJS
          3. Moving from Node.js to the browser with Browserify
        4. Delivering HTML templates
          1. Defining the templates in script tags
          2. Loading the template externally
          3. Writing HTML inside the JavaScript
          4. Precompiling templates
        5. Summary
      11. 4. Developing the Model-View-Controller Layers
        1. Evolving the current setup
          1. Directory structure
          2. Forming the main server handlers
        2. Implementing the router
        3. Introducing Ractive.js
        4. Constructing the entry point of the application
        5. Defining a controller
        6. Managing our views
        7. Creating a model
        8. Summary
      12. 5. Managing Users
        1. Working with the MongoDB database
          1. Installing MongoDB
          2. Running MongoDB
          3. Connecting to the database server
        2. Extending the code from the previous chapter
          1. Updating our base model class
          2. Updating page navigation and routing
        3. Registering a new user
          1. Updating the frontend
          2. Updating the backend API
        4. User authentication with sessions
        5. Managing a user's profile
        6. Summary
      13. 6. Adding Friendship Capabilities
        1. Finding friends
          1. Adding the search page
          2. Writing the model
          3. Fetching friends from the database
        2. Marking users as friends
        3. Displaying the linked users on the Profile page
        4. Summary
      14. 7. Posting Content
        1. Posting and storing text
          1. Adding a form to post text messages
          2. Introducing the content's model
          3. Updating the controller of the home page
          4. Storing content in the database
        2. Showing the user's feed
        3. Posting files
        4. Summary
      15. 8. Creating Pages and Events
        1. Refactoring the API
        2. Adding a form to create pages
        3. Creating a record in the database
        4. Showing the currently added pages
        5. Showing a specific page
        6. Posting a comment to a page
        7. Showing the comments
        8. Managing events attached to a particular page
        9. Summary
      16. 9. Tagging, Sharing, and Liking
        1. Selecting friends and sending their IDs to the backend
        2. Storing the tagged users and displaying them in the user's feed
          1. Sharing a post
        3. Liking posts and counting the number of likes
        4. Showing the number of likes
        5. Summary
      17. 10. Adding Real-time Chat
        1. Getting to know WebSockets
        2. Bringing Socket.IO to the project
        3. Preparing the UI of the chat area
        4. Exchanging messages between the client and the server
        5. Sending messages to the user's friends only
        6. Customizing the output of the chat
        7. Summary
      18. 11. Testing the User Interface
        1. Introducing the basic testing toolset
          1. The testing framework
          2. The assertion library
          3. Runner
        2. Preparing our project to run tests
        3. Running our test with PhantomJS
        4. Testing user registration
          1. Simulating user interaction
          2. Filling and submitting the registration form
          3. Tweaking the code's execution order
          4. Listening to the form-submitted event
        5. Testing with DalekJS
          1. Installing DalekJS
          2. Using the DalekJS API
        6. Summary
      19. Index