Skip to content

Instantly share code, notes, and snippets.

@andineck
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andineck/9410473 to your computer and use it in GitHub Desktop.
Save andineck/9410473 to your computer and use it in GitHub Desktop.
Erste nodejs/expressjs app
# 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
# 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
# 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
# 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.
# 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