Prerequisits
node.js installednpm installed
Install express globaly
$ npm install -g expressCreate a npm project
Create your project rootfolder "express-project" and open a terminal in that folder and typpe:$ npm init
Follow the intstructions on your terminal.
Add needed dependencies
Open package.json and add your dependencies. My package.json looks like that:
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
{ | |
"name": "simple-express", | |
"version": "1.0.0", | |
"description": "simple exress sample", | |
"main": "app.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [ | |
"exptress" | |
], | |
"author": "peboethig", | |
"license": "MIT", | |
"dependencies": | |
{ | |
"node-mailer":"*", | |
"body-parser":"*", | |
"express":"*", | |
"jade":"*" | |
} | |
} |
You can just pick the "dependencies" object
Add you dependencies
Create a file "app.js" and paste following code
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
var express = require("express"); | |
var path = require("path"); | |
var bodyParser = require("body-parser"); | |
var nodeMailer = require("'nodemailer"); | |
var app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended:false})); | |
app.get("/", function(req, res) { | |
console.log("hello world"); | |
res.send("hello world"); | |
}); | |
app.listen(3000, function() { | |
console.log("App listening on port 3000!"); | |
}); |
As you can see, the webserver will be bound on port 3000 and it will return a simple "Hello world" response and will log the same words to the console.
Start the webserver
Open a terminal in the projectroot and type:$ node app.js
Now you can surf http://localhost:3000 and you should see "Hello world"
Thats it
Keine Kommentare:
Kommentar veröffentlichen