-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
410beba
commit 480b3c2
Showing
2 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// view | ||
var view = { | ||
render: function() { | ||
var html = ''; | ||
html += '<!DOCTYPE html>'; | ||
html += '<html>'; | ||
html += '<head><title>Nodejs example</title></head>'; | ||
html += '<body>'; | ||
html += '<h1>Status ' + (model.status ? 'on' : 'off') + '</h1>'; | ||
html += '<a href="/on">switch on</a><br />'; | ||
html += '<a href="/off">switch off</a>'; | ||
html += '</body>'; | ||
html += '</html>'; | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(html + '\n'); | ||
} | ||
}; | ||
|
||
// model | ||
var model = { | ||
status: false, | ||
update: function(s) { | ||
this.status = s; | ||
view.render(); | ||
} | ||
}; | ||
|
||
// controller | ||
var http = require('http'), res; | ||
var controller = function(request, response) { | ||
res = response; | ||
if(request.url === '/on') { | ||
model.update(true); | ||
} else if(request.url === '/off') { | ||
model.update(false); | ||
} else { | ||
view.render(); | ||
} | ||
} | ||
http.createServer(controller).listen(1337, '127.0.0.1'); | ||
console.log('Server running at http://127.0.0.1:1337/'); |