As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications
nodejs is a javascript runtime which allows you to execute server side javascript scripts and applications
The most basic javascript script
write script into file script.js
execute script
console output
nodejs ships with a big standard library which covers a lot of filesystem, text processing and network APIs
In addition to the standard library nodejs ships with a built-in package management system which allow you to install 3rd party packages to extend the functionality of the standard library
nodejs is based googles V8 javascript engine and thus has the same limitations regarding threading behavior as the browser engine
Disclaimer: Just because of nodejs is capable of doing a task its not always the best tool
Important: because nodejs supports ES Modules, I will only use the ES module syntax and don't dive into commonjs
A nodejs packages consists of a directory which has at least on file
package.json
this file is mandatory and describes the package<filename>.js
an arbitrary number of javascript modulesnode_modules
folder for dependencies (only after installing)
A simple package description
name of the package
type of the javascript files inside the package, in this case es modules
version of the package, must comply semantic versioning standard
main entry point for bare imports
description of the package
object with a list of packages our package depends on
sample content of index.js
file
import a function provided by the standard library (built-in module)
bare import a function provided by a third party packages (located in node_modules)
relative import a function from a module of your package
export a function to be used by other modules or packages
creation of new package, create a folder and run npm init
within the folder and
follow the instructions
In general there are two types of packages, library packages which provides additional functionality and only exports functions, constants, etc.. and application packages which contain an application which can be run directly
nodejs brings its own tool for managing packages and dependencies, also there is a global registry provided under https://www.npmjs.com/ where you can publish your own packages and get 3rd party packages
npm
is the built in command line utility for node to manage and install
packages and dependencies
To install a new package you just have to execute npm install <package>@<version>
within your package where the
package.json file is located
the <version> path can be omitted if you want the latest published version
You can also manually edit the package.json file and add dependencies
packages which your package depends upon will be saved to the node_modules
folder next to your package.json. The node package resolution algorithm will scan this folder if you use a
bare import within your javascript
depending on the amount of your dependencies and transitive dependencies of your dependencies the folder can get quite big
node packages have three types depdendencies
dependencies are runtime dependencies which are needed during the execution of your package
devDependencies are dependencies which are needed only during the development of your package or for building it
typescript, module bundler, linter, etc... for example
peerDependencies are dependencies which are provided by the consumer of your package
usually used if you write a plugin to a library, then the consumer must add the package your library extends
The dependency objects are key value pairs with the package name as key and the version as value
In this case the package depends on the package ws
with version [7.3.0, 8.0.0)
create a folder and a package.json file
add module
type to package.json
open index.js and create a new webserver
import the http
package from the standard library
define the host on which the server listens and a port
create a new server and listen to requests
create a response to every requests which resturns 'hello world'
start server
If we now start the server and try to access it we get 'hello world' as a response
Now we want top be able to serve static files
import helpers for file system access and path handling
we parse the provided url and rewrite it to "index.html" if no path provided
We need to make tha paths relative passed as an url, so we need to remove the leading /
If the file exists we read it and return the content in the response
Now if we create a index.html within our project and navigate to http://127.0.0.1:3000
the index.html will be displayed in a browser
Until now we only support html files, we need to add a function to determine the right MIME-Type and return it as Content-Type
We need to determine the content type by the provided file extension
return the correct mime type for a specific file extension
we can also overwrite specific paths to return the greeting from the first example
unfortunately nodejs does not support websockets out of the box so we need to add a 3rd party library
because the ws
package is a older commonjs module we can import it but we need
to access the exports via the "default" property
to establish a websocket connection we forward an "upgrade" request to our websocketServer which will create a small echo service