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 8.9.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
to access the REPL, try out some JS! Use .exit
to exitnode [YOUR_FILE].js
cd
into itnpm init
npm init --yes
?Node.js contains many useful core modules for us to use, such one to use the file system
const fs = require('fs');
fs.readFile('ourfile.txt', 'utf8', function(err, data) {
// do things
});
const data = fs.readFileSync('ourfile.txt', 'utf8');
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
node_modules
folderpackage.json
npm install express
npm install express
npm install express lodash ...
Tell npm to save a package to your package.json, or install multiple packages at once!
npm install
is smart!
npm install foo/foo
npm install ../foo
npm install foo@1.0
npm install -g foo
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.
npm install jest --save-dev
npm install jest -D
You can install packages as dev dependencies, meaning people who run the built package don't require these modules.
In the Node.js module system, each file is treated as a separate module.
A package describes a group of 1+ modules
var express = require('express');
npm install express
node_modules
folder, and run npm install
. npm installs your defined dependency!
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!');
});
Exercise solutions at https://github.com/pselle/Intro-to-Node.js-Exercises