How do I setup a server side JavaScript
Solution 1:
"Server-side JavaScript" usually refers to Node.js (or the Node fork, io.js, which is being merged back into the Node project).
What is Node.js?
On its own, Node is not a server. It also isn't a language. At its core, Node is just an environment in which you can run JavaScript code. They've taken Google's open source V8 JavaScript engine (which is used in Chrome) and separated it from the browser so you can run JavaScript outside of a web browser.
Node comes bundled with some neat tools to help you interact with the filesystem and HTTP requests. There's also a huge repository of third-party modules available through npm.
Further reading:
- About Node.js
- Node.js on Wikipedia
Setting up Node.js on Ubuntu
There are several ways to get Node working on Ubuntu, each with pros and cons.
-
Install from the official repos
- How:
sudo apt-get install nodejs
- Pro: It's easy
- Con: The package is always badly out-of-date
- Con: Lots of scripts expect to find
node
in your path, but Ubuntu usesnodejs
so you have to create aliases. - Con: You install with
sudo
which means that globally-installed third-party packages from npm also have to be installed withsudo
. Sketchy.
- How:
-
Install via the NodeSource repository
- How: Follow these instructions
- Pro: Your Node will be up-to-date
- Pro: This is the semi-officially recommended method of installation
- Con: You still have to use
sudo
to install other modules
-
Install via the Node Version Manager
- How: Follow these instructions
- Pro: Installs in your home directory, so no weird permissions
- Pro: Easy to keep node up-to-date, or even run multiple version of Node
- Con: Node is tied to your user account, which isn't great if you're setting up a production server
- Con: It's your responsibility to keep Node updated
Creating a Server
Remember that Node itself is not a web server, however it comes with lots of handy tools for building web servers. Without any additional packages, you can save this example server (taken from the Node.js About page) as server.js
and run it from the command line with node server.js
(or nodejs server.js
, depending on how you installed Node).
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
However, unless you were hoping to send plain-text responses, you'll probably want to look into a more robust solution such as Express.
Any further training at this point really depends on what you want to do with Node (and things become much less Ubuntu-specific). NodeSchool has great tutorials on everything from the basics and managing packages to web servers and databases.
And finally, a few popular packages to help you solve common problems:
- socket.io for handling real-time interactions with web sockets
- Passport for authenticating users
- Mongo for data storage in a document-oriented (NoSQL) database