Book Image

Internet of Things Programming with JavaScript

Book Image

Internet of Things Programming with JavaScript

Overview of this book

The Internet of Things is taking the tech world by storm, and JavaScript is at its helm. This book will get you to grips with this exciting new technology. Where do Node.js, HTML5 and Windows 10 IoT Core come in with JavaScript and IoT? Why Raspberry Pi Zero rather than Arduino? How do you configure and build an IoT network from scratch? All your IoT JavaScript questions are answered in this book.
Table of Contents (15 chapters)
Internet of Things Programming with JavaScript
Credits
About the Author
www.packtpub.com
Customer Feedback
Preface

Making a simple web server with Node.js


One of the most important aspect of having a Raspberry Pi is that we have a real computer configured with services and servers. In this section, we will explain how to install Node.js, which is a powerful framework that we will use to run most of the applications we are going to see in this book. Luckily for us, installing Node.js on Raspberry Pi is really simple.

In the folder for this chapter, open the file called webserver.js. We will create a server on port 8056. To test the program and see the results we have to open the Node.js terminal on your MS-DOS interface and run this file with the following command:

node webserver.js

Add the following lines to webserver.js file to declare the HTTP request commands:

var http = require('http'); 

We create the server with the following function:

http.createServer(function (req, res) { 

We define the content of the file that we will show in the HTML code:

res.writeHead(200, {'Content-Type': 'text/plain...