Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechThomas committed May 10, 2019
1 parent 1216653 commit b358726
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
25 changes: 25 additions & 0 deletions static_content_routes_api/app.js
@@ -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);
});
10 changes: 10 additions & 0 deletions static_content_routes_api/package.json
@@ -0,0 +1,10 @@
{
"name": "static_content_routes_api",
"private":"true",
"scripts":{
"start":"node app.js"
},
"dependencies":{
"express":"^5.0.0"
}
}
44 changes: 44 additions & 0 deletions static_content_routes_api/routes/api1.js
@@ -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;
1 change: 1 addition & 0 deletions static_content_routes_api/static/file.txt
@@ -0,0 +1 @@
Hello from file!

0 comments on commit b358726

Please sign in to comment.