nodejs docs
https://nodejs.org/dist/latest-v14.x/docs/api/
https://www.npmjs.com/
npm will manage your project's node packages for you.
node -v && npm -v
Check what packages you have installed globally or within your project.
npm list -g --depth=0 2>/dev/null
npm list --depth=0 2>/dev/null
We'll install some global npm packages.
# tools
npm install -g serve
npm install -g typescript
npm install -g eslint
mkdir cwc-macdevsetup
cd cwc-macdevsetup
We'll use this to test out node.
touch sandbox.js
let x = 1;
let y = 10;
let z = '1';
console.log('x + y', x + y);
console.log('x == z', x == z);
console.log('x === z', x === z);
run sandbox
node sandbox.js
Using the http module to serve up a page.
touch index.js
index.js
const http = require('http');
const port = 3000;
const server = http.createServer((req, res) => {
//http:localhost:3000
if (req.url === '/') {
res.write('Hello Coders!');
res.end();
}
//http:localhost:3000/about
if (req.url === '/about') {
res.write('About Coders');
res.end();
}
//http:localhost:3000/api/data
if (req.url === '/api/data') {
res.write(JSON.stringify([1, 'two', 'three', 4]));
res.end();
}
});
server.listen(port);
console.log(`Listening on port ${port}`);
run our index
node index.js
Setup a our first express App.
Express is a node application framework
npm init -y
npm install express
touch express.js
const express = require('express');
const app = express();
const port = 3001;
//http:localhost:3001
app.get('/', (req, res) => {
res.send('Hello EXPRESS Coders!');
});
app.listen(port, () => {
console.log(`Express App on port ${port}`);
});
run our express app
node express.js