Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Mackie committed May 20, 2022
0 parents commit ba2f96f
Show file tree
Hide file tree
Showing 150 changed files with 5,887 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.swp
*.xml~
*.rb~
*.css~
*.js~
tmp/
log/
.DS_Store

19 changes: 19 additions & 0 deletions ch03/console/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node

// fetch name from command argument, environment, or fallback
const nameArg = capitalize(process.argv[2] || process.env.USER || process.env.USERNAME || 'world');

// output message
console.log(`Hello ${ nameArg }!`);

// capitalize the first letter of all words
function capitalize(str) {

return str
.trim()
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');

}
30 changes: 30 additions & 0 deletions ch03/server/webhello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

const
port = (process.argv[2] || process.env.PORT || 3000),
http = require('http');

http.createServer((req, res) => {

console.log(req.url);
const nameArg = capitalize( req.url.replace(/[^\w.,-]/g, ' ').replace(/\s+/g, ' ').trim() || 'world' );

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(`<p>Hello ${ nameArg }!</p>`);

}).listen(port);

console.log(`Server running at http://localhost:${ port }/`);

// capitalize the first letter of all words
function capitalize(str) {

return str
.trim()
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');

}
21 changes: 21 additions & 0 deletions ch04/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"name": "nodemon",
"program": "${workspaceFolder}/webhello.js",
"request": "launch",
"restart": true,
"runtimeExecutable": "nodemon",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node"
}
]
}
37 changes: 37 additions & 0 deletions ch04/webhello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

const
port = (process.argv[2] || process.env.PORT || 3000),
http = require('http');

http.createServer((req, res) => {

// abort favicon.ico request
if (req.url.includes('favicon.ico')) {
res.statusCode = 404;
res.end('Not found');
return;
}

// handle other requests
const nameArg = capitalize( req.url.replace(/[^\w.,-]/g, ' ').replace(/\s+/g, ' ').trim() || 'world' );

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(`<p>Hello ${ nameArg }!</p>`);

}).listen(port);

console.log(`Server running at http://localhost:${ port }/`);

// capitalize the first letter of all words
function capitalize(str) {

return str
.trim()
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');

}
15 changes: 15 additions & 0 deletions ch05/express01/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Express Web Application Example

To run this example, navigate to this directory in your terminal then install the Node.js dependencies with:

```sh
npm install
```

Run the application with:

```sh
npm start
```

Then open paths at <http://localhost:3000/> in your web browser.
26 changes: 26 additions & 0 deletions ch05/express01/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Express.js application
import express from 'express';

// configuration
const
cfg = {
port: process.env.PORT || 3000
};

// Express initiation
const app = express();

// home page route
app.get('/', (req, res) => {
res.send('Hello World!');
});

// another route
app.get('/hello/', (req, res) => {
res.send('Hello again!');
});

// start server
app.listen(cfg.port, () => {
console.log(`Example app listening at http://localhost:${ cfg.port }`);
});
15 changes: 15 additions & 0 deletions ch05/express01/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "express",
"version": "1.0.0",
"description": "Example Express.js app",
"type": "module",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "Craig Buckler",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
}
}
15 changes: 15 additions & 0 deletions ch05/express02/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Express Web Application Example

To run this example, navigate to this directory in your terminal then install the Node.js dependencies with:

```sh
npm install
```

Run the application with:

```sh
npm start
```

Then open paths at <http://localhost:3000/> in your web browser.
60 changes: 60 additions & 0 deletions ch05/express02/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Express.js application
import express from 'express';
import compression from 'compression';

import { fileURLToPath } from 'url';
import { dirname, sep } from 'path';

// configuration
const
__dirname = dirname(fileURLToPath( import.meta.url )) + sep,
cfg = {
port: process.env.PORT || 3000,
dir: {
root: __dirname,
static: __dirname + 'static' + sep
}
};

console.dir(cfg, { depth: null, color: true });

// Express initiation
const app = express();

// do not identify Express
app.disable('x-powered-by');

// HTTP compression
app.use( compression() );

// log every request to the terminal
app.use((req, res, next) => {
console.log(req.url);
next();
});

// home page route
app.get('/', (req, res) => {
res.send('Hello World!');
});

// another route
app.get('/hello/', (req, res) => {
res.send('Hello again!');
});

// serve static assets
app.use(express.static( cfg.dir.static ));

// 404 errors
app.use((req, res) => {
res.status(404).send('Not found');
});

// start server
app.listen(cfg.port, () => {
console.log(`Example app listening at http://localhost:${ cfg.port }`);
});

// export defaults
export { cfg, app };
16 changes: 16 additions & 0 deletions ch05/express02/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "express",
"version": "1.0.0",
"description": "Example Express.js app",
"type": "module",
"main": "index.js",
"scripts": {
"start": "nodemon index.js"
},
"author": "Craig Buckler",
"license": "MIT",
"dependencies": {
"compression": "^1.7.4",
"express": "^4.17.1"
}
}
13 changes: 13 additions & 0 deletions ch05/express02/static/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Static page</title>
<meta name="viewport" content="width=device-width,initial-scale=1" />
</head>
<body>

<h1>This is a static page</h1>

</body>
</html>
15 changes: 15 additions & 0 deletions ch05/express03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Express Web Application Example

To run this example, navigate to this directory in your terminal then install the Node.js dependencies with:

```sh
npm install
```

Run the application with:

```sh
npm start
```

Then open paths at <http://localhost:3000/> in your web browser.
65 changes: 65 additions & 0 deletions ch05/express03/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Express.js application
import express from 'express';
import compression from 'compression';

import { fileURLToPath } from 'url';
import { dirname, sep } from 'path';

// configuration
const
__dirname = dirname(fileURLToPath( import.meta.url )) + sep,
cfg = {
port: process.env.PORT || 3000,
dir: {
root: __dirname,
static: __dirname + 'static' + sep,
views: __dirname + 'views' + sep,
routes: __dirname + 'routes' + sep,
}
};

console.dir(cfg, { depth: null, color: true });

// Express initiation
const app = express();

// do not identify Express
app.disable('x-powered-by');

// use EJS templates
app.set('view engine', 'ejs');
app.set('views', cfg.dir.views);

// HTTP compression
app.use( compression() );

// log every request to the terminal
app.use((req, res, next) => {
console.log(req.url);
next();
});

// home page route
app.get('/', (req, res) => {
res.render('message', { title: 'Hello World!' });
});

// /hello/ route
import { helloRouter } from './routes/hello.js';
app.use('/hello', helloRouter);

// serve static assets
app.use(express.static( cfg.dir.static ));

// 404 errors
app.use((req, res) => {
res.status(404).render('message', { title: 'Not found' });
});

// start server
app.listen(cfg.port, () => {
console.log(`Example app listening at http://localhost:${ cfg.port }`);
});

// export defaults
export { cfg, app };
12 changes: 12 additions & 0 deletions ch05/express03/lib/locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// localisation

// international greetings
export const hello = {
au: 'G\'day',
cn: 'Nǐ hǎo',
en: 'Hello',
de: 'Hallo',
es: 'Hola',
fr: 'Bonjour',
jp: 'Kon\'nichiwa'
};
13 changes: 13 additions & 0 deletions ch05/express03/lib/string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// string functions

// capitalize the first letter of all words
export function capitalize(str) {

return str
.trim()
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');

}
Loading

0 comments on commit ba2f96f

Please sign in to comment.