Skip to content

Commit

Permalink
Structure: nodejs 3 basic layers
Browse files Browse the repository at this point in the history
  • Loading branch information
lecaoquochung committed Jul 22, 2016
1 parent c4a46e8 commit 1848365
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,18 @@ exports.coding = function() {
- child process

## PROJECT ARCHITECTURE
### Nodejs application basic layers
- Fundamental: Process the Request itself (basically difference with PHP)
- 1st layer: Routing -> parses the request URL & decides what to do
- 2nd layer: Static -> Place logic, send respond...
- 3rd layer: Backend logic
- All base on the the URL
- Example of 3 basic layers in nodejs

### Task runner & building system

### Test-driven development

### MVC

### REST API
3 changes: 3 additions & 0 deletions example/assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
9 changes: 9 additions & 0 deletions example/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Page HTMl </title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
This is node js test page.html
</body>
</html>
25 changes: 25 additions & 0 deletions example/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
var content = '';
var type = '';

if(req.url === '/') {
content = fs.readFileSync('./page.html');
type = 'text/html';
} else if(req.url === '/styles.css') {
content = fs.readFileSync('./assets/style.css');
type = 'text/css';
} else if(req.url === '/api/user/new') {
// Do action
// POST parameter
// save to db
content = '{"success": true}';
type = 'application/json';
}

res.writeHead(200, {'Content-Type': type});
res.end(content + '\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337');

0 comments on commit 1848365

Please sign in to comment.