Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
How your computer accesses websites
But ALSO how computers talk to each other
JavaScript (in our context) is "client side"
You can run JavaScript on the server with the help of Node.js!
nvm install 4
node -v
in your terminal and see what it saysnpm install npm@latest -g
npm -v
in your terminal and see what it saysnode [YOUR_FILE].js
cd
into itnpm init
npm init --yes
?package.json
is a JSON file that defines information about your app, and npm knows about it
There are some rules around names and other parts of your package.json, and npm will warn you if it doesn't like something
A dependency is something your application needs to run (it depends on it!).
You might install a dependency that relies on something else, adding it to your dependency chain
.
Check out the dependency chain for express.
Some dependencies are only used in development mode (vs production), and these are known as devDependencies
in the package.json file.
var express = require('express');
npm install --save express
app.js
file and require express
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Welcome to the GDI Node Workshop!');
});
app.listen(3000, function () {
console.log('Your app is listening on port 3000!');
});
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
npm scripts allow you to use npm to manage a lot of your app using easy commands like npm start
, npm test
and more.
People use "scripts" in package.json to: compile or preprocess code, run tests, build for a deployment or environment, or really anything!
If you define an arbitrary key (not in npm docs), you can run it using npm run KEY
"scripts": {
...
"stuff": "echo \"LET'S DO STUFF\""
}
...
$ npm run stuff
app.js
to run a simple web server using ExpressThey're what you get back from a request π
"Routes" define how a server should respond to a certain request at a certain end point or path.
// app.<HTTP action>
app.get('/', function (req, res) {
res.send('Hello World!');
});
Our controllers mediate communication between our models and views
exports.index = function (req, res) {
res.send('Welcome to the GDI Node Workshop!');
}
Node modules do something and expose things to the outside world with exports
exports.index = function (req, res) {
res.send('Welcome to the GDI Node Workshop!');
}
var express = require('express');
// var nameWeUseInOurApp = require('someModule');
// Tell app.js about the home controller using require
var homeController = require('./controllers/home');
// Routes
app.get('/', homeController.index);
home.js
file in a controllers
folder.home.js
app.js
to use your controller./*
route as a catch-all displays a 404 message./about
Pass data through to your view
var path = require('path');
exports.index = function (req, res) {
res.sendFile(path.join(__dirname, '../public', 'templates', 'index.html'));
}
We need to tell Express which files we allow the user to access
// Serve static files (i.e. images, scripts, styles, templates) from public/ directory
app.use(express.static('public'));
index.html
/about
if you haven't yet/about
with a new templateBecause writing HTML can be a lot! And view engines are great!
Check out Pug (formerly known as Jade)'s site for examples and an interactive playground
// template.pug
h1 Welcome
a(href="somewhere.com") Some cool link!
Install & Use Pug
npm install --save pug
// in app.js
app.set('view engine', 'pug');
Use res.render
to use your view engine
exports.about = function (req, res) {
res.render('about');
}
Since we put our about.pug
file in views/
Express knows where that is and assumes that's what we mean when we say render
npm install --save-dev nodemon
and update your package.json
to use nodemon to run app.js
. It will live reload so you don't have to start/stop your server!
.pug
views to a views
folderApplication Programming Interface
AKA computers talk to each other
APIs allow us to request data from a service
Some APIs you might want to use
/api/all
/api/get/:id
where id is the index of the dinosaurExercise solutions at https://github.com/pselle/Intro-to-Node.js-Exercises