Creating an Express Project - abbernie/web-dev-sp17 GitHub Wiki
Create an empty folder that will contain your project.
Open Terminal and navigate to your folder. Use cd
to change directory until you are inside the directory you want.
Initialize the Node project:
npm init
You will be asked a series of questions. Hit Enter after each one to accept the defaults.
Install Express:
npm install --save express
Create a javascript file and save it in your project folder. Call the file whatever you like; app.js
and index.js
are common. In your new file, there are a few basics that almost all express projects will need:
// bring in the Express module
var express = require('express');
// create a new instance of express
var app = express();
// ** Routes ** //
// routes will go here
// Start the server. Listen for traffic on port 3000
app.listen(3000, function () {
// Print out a message to the console
console.log('Listening on port 3000!');
});
This code will function, but won't be of use to us until we add in some routes. Under the // ** Routes ** //
comment, create your first route:
// Set up the home page
app.get('/',function(request,response){
// respond with "Hello world"
response.send('Hello world');
});
Now customize and create some routes of your own. Another example route:
app.get('/blog',function(request,response){
response.send('This is my blog');
});
Save your app.js
file (or whatever you chose to call it) and go back to Terminal. Navigate to the folder where your file resides. Start the server by typing:
node app.js
If you have no errors, you should see a message that says Listening on port 3000!
. Open a browser window and enter localhost:3000
in the address bar. You should see the message you output for the "/" route. If you're following this example, it will be "Hello World".
If you created additional routes, like the "blog" route in my example above, test those out too. Go to localhost:3000/blog
.
Putting it online
When you're ready to put your project online, upload all your files -- your javascript file, and also package.json and the node_modules folder -- to Digital Ocean via FTP using Cyberduck.
In Terminal, log in to Digital Ocean using ssh
. You'll type: ssh [email protected]
-- except with your IP address instead of 123.45.678. You'll be prompted for your password. Note that when you enter your password here, you won't see your typing.
When you're logged into Digital Ocean, navigate to the folder where you upload your files. Remember, you can use ls
to list the files in the directory you're in and cd NAMEOFDIRECTORY
to change directory (where NAMEOFDIRECTORY is the name of the directory you want to go to).
Now you're ready to run your script on Digital Ocean. Just like you did on your local computer, type:
node app.js
(Replacing "app.js" with the name of your script if you called it something different.)
Check and make sure it's working. Go to your IP address in a browser, making sure to add the port number. For example, if your code uses port 3000
and your IP address is 12.345.67.89
, you'd go to 12.345.67.89:3000
in the browser.
See Keep It Running for how to keep your script running even after you log out of Digital Ocean or close Terminal.