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
1216653
commit b358726
Showing
4 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//Załadowanie potrzebnych modułów | ||
var express = require('express'); | ||
var morgan = require('morgan'); | ||
var path = require('path'); | ||
var api1Module = require('./routes/api1') | ||
|
||
|
||
// Utworzenie aplikacji modułu express | ||
var app = express(); | ||
|
||
app.use(morgan('short')); | ||
|
||
var staticPath = path.join(__dirname, 'static'); | ||
app.use("/static", express.static(staticPath)); | ||
|
||
app.use("/v1", api1Module); | ||
|
||
app.use(function(req,res){ | ||
res.status(404); | ||
res.send("File not found!"); | ||
}) | ||
|
||
var server = app.listen(3000, function () { | ||
console.log("Server running at port: %s", server.address().port); | ||
}); |
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,10 @@ | ||
{ | ||
"name": "static_content_routes_api", | ||
"private":"true", | ||
"scripts":{ | ||
"start":"node app.js" | ||
}, | ||
"dependencies":{ | ||
"express":"^5.0.0" | ||
} | ||
} |
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,44 @@ | ||
var express = require('express'); | ||
var bodyParser = require('body-parser'); | ||
var jsonParser = bodyParser.json(); | ||
|
||
var api = express.Router(); | ||
|
||
api.use("/user/:id", function(req,res,next){ | ||
var userId = parseInt(req.params.id, 10); | ||
if(userId < 1) { | ||
console.log(userId); | ||
res.status(404).send("User not found!"); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
|
||
api.get("/user/:id", function(req, res){ | ||
var userId = parseInt(req.params.id, 10); | ||
res.status(200); | ||
res.json({"userId":userId,"status":"existing"}); | ||
}); | ||
|
||
api.delete("/user/:id", function(req,res){ | ||
var userId = parseInt(req.params.id, 10); | ||
res.status(200); | ||
res.json({"userId":userId,"status":"deleted"}); | ||
}); | ||
|
||
api.post("/user/:id", jsonParser, function(req,res){ | ||
var userId = parseInt(req.params.id, 10); | ||
var name = req.body.name; | ||
res.status(200); | ||
res.json({"userId":userId,"status":"created","name":name}); | ||
}); | ||
|
||
api.put("/user/:id", jsonParser, function(req,res){ | ||
var userId = parseInt(req.params.id, 10); | ||
var name = req.body.name; | ||
console.log("Updated user: " + name); | ||
res.status(200); | ||
res.json({"userId":userId,"status":"updated","newName": name}); | ||
}); | ||
|
||
module.exports = api; |
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 @@ | ||
Hello from file! |