Last active
August 29, 2015 13:57
-
-
Save andineck/9410473 to your computer and use it in GitHub Desktop.
Erste nodejs/expressjs app
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
# Raspberry PI Version herausfinden: | |
cat /etc/issue | |
# -> Debian GNU/Linux 7 \n \l | |
oder: | |
cat /etc/*-release | |
# -> PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)" | |
# install node.js on raspberry pi (wheezy) | |
# https://github.com/midnightcodr/rpi_node_install: | |
# tested up to v0.10.24 | |
# dauert lange: etwa 3 stunden :-| | |
curl -L https://raw.github.com/midnightcodr/rpi_node_install/master/setup.sh | bash -s 0.10.24 | |
node -v | |
# -> v0.10.24 | |
npm -v | |
# -> 1.3.21 | |
# installation von weiteren Paketen: | |
# python (~v2.7) muss installiert sein | |
sudo apt-get install make gcc | |
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
# installiere node/npm | |
# npm ist der paketmanager | |
# test: | |
node -v | |
npm -v | |
# node api: http://nodejs.org/api/ | |
# npm api: https://www.npmjs.org/doc/ | |
# installiere express global (-g): web/routing framework: | |
npm install express -g | |
# lege dein projekt an | |
mkdir koni && cd koni | |
# generiere die grundstruktur deiner web applikation: (express -h) für hilfe: | |
# api: http://expressjs.com/3x/api.html | |
express -e | |
# installiere die abhängigkeiten für deine neue webapp (dependencies in package.json): | |
npm install | |
# starte deine neue web app: | |
node app.js | |
# betrachte die web app im browser: | |
# http://localhost:3000 | |
# oder in neuem terminal: | |
curl http:localhost:3000 |
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
# folgendes in app.js einfügen | |
# neue route für properties einfügen | |
app.get('/properties', function(req, res) { | |
res.send('send properties'); | |
}); | |
# nodejs neu starten: ctrl+c | |
node app.js | |
# staunen :-) | |
curl http://localhost:3000/properties |
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
# neue view einfügen | |
cd views && touch properties.ejs | |
# datei properties.ejs editieren und folgendes einfügen: | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<h1>Welcome to <%= title %></h1> | |
<ul> | |
<% for (var prop in properties) { %> | |
<li><%= prop %>: <%= properties[prop] %></li> | |
<% } %> | |
</ul> | |
</body> | |
</html> | |
# neue route in app.js eintragen: | |
app.get('/props', function(req, res) { | |
res.render('properties', { | |
title: 'MR PROPS', | |
properties: ['entrance door', 'window1', 'window2'] | |
}); | |
}); | |
# nun wird das template properties.ejs beim aufruf der route '/props' gerendert mit dem kontext objekt mit den properties title und properties | |
# api für ejs template: https://github.com/visionmedia/ejs | |
# grundsätzlich kann mit <% ...code... %> javascript code eingefügt werden, und mit <%= ...werte... %> werte ausgegeben werden. | |
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
# TODO: JSON Datei aus Filesystem auslesen, parsen und inhalt in html einfügen und senden |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment