Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express') // Popular web framework for Node.js
- var bodyParser = require('body-parser') // Auto parses body of post and put requests
- var morgan = require('morgan') // Logs HTTP requests to the console
- var app = express()
- app.use(express.static('static')); // makes the files in /static folder available
- app.use(bodyParser.urlencoded({ extended: true })) //
- app.use(bodyParser.json()); // Hooking on functions that will we called for each request
- app.use(morgan('dev')); //
- var data = require('./data');
- var port = 19201;
- // HTTP SERVER
- app.listen(port, function () {
- console.log('Message app listening on port '+port+'!')
- });
- // HOME PAGE
- app.get('/', function (request, response) {
- response.sendFile(__dirname + '/index.html');
- });
- // MESSAGES ENDPOINT
- app.post('/messages', function(request, response) {
- console.log(request.body);
- _username = request.body.userName;
- _text = request.body.text;
- if(_username == "" || _text == "" )
- {
- response.sendStatus(400);
- }
- else
- {
- data.addMessage(_username, _text);
- response.sendStatus(201);
- }
- });
- app.delete('/messages/:id', function(request, response)
- {
- var _id = request.params.id;
- if(data.removeMessage(_id) == null)
- {
- response.sendStatus(404);
- }
- else
- {
- response.json(data.removeMessage(_id));
- response.sendStatus(200);
- }
- })
- app.get('/messages', function(request, response)
- {
- response.json(data.messages);
- });
- app.get('/messages/:id', function(request, response)
- {
- var id = request.params.id;
- if(data.getMessageById(id)==null)
- {
- response.sendStatus(404);
- }
- else
- {
- response.json(data.getMessageById(id))
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement