Prerequisits
node.js installednpm installed
Create a webserver
This webserver can dispatch a hand full of images , text, javascript and css
Create a file "server.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
const http = require('http'); | |
const url = require('url'); | |
const path = require('path'); | |
const fs = require('fs'); | |
const mimeTypes = | |
{ | |
"html":"text/html", | |
"jpg":"image/jpg", | |
"jpeg":"image/jpeg", | |
"png":"image/png", | |
"css":"text/css", | |
"javascript":"text/javascript" | |
} | |
http.createServer(function(req, res){ | |
var uri = url.parse(req.url).pathname; | |
console.log(uri); | |
var fileName = path.join(process.cwd(), unescape(uri)); | |
console.log('Loading' + uri); | |
var stats; | |
try | |
{ | |
stats = fs.lstatSync(fileName); | |
} | |
catch(e) | |
{ | |
res.writeHead(404,{'Content-Type':'text/plain'}); | |
res.write('404 Not found\n'); | |
res.end(); | |
return; | |
} | |
if(stats.isFile()) | |
{ | |
var mimeType = mimeTypes[path.extname(fileName).split(".").reverse()[0]]; | |
res.writeHead(200,{'Content-type': mimeType}); | |
var fileStream = fs.createReadStream(fileName); | |
fileStream.pipe(res); | |
}else if(stats.isDirectory()) | |
{ | |
res.writeHead(302,{'location':'index.html'}); | |
}else | |
{ | |
res.writeHead(500, {'Content-type':'text/plain'}); | |
res.write('500 internal Error\n'); | |
res.end; | |
} | |
}).listen(1337); |
This will create the webserver
Create a file to dispatch
Open a terminal in the projectroot and create a file "index.html" with following content
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
<html> | |
<head></head> | |
<body> | |
<h1>Testing</h1> | |
</body> | |
</html> |
Start the webserver
Open a terminal in the projectroot and type:$ node server.js
Now you can surf http://localhost:1337/index.html and you should see "Testing"
Thats it
Keine Kommentare:
Kommentar veröffentlichen