This file contains hidden or 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
<?php | |
// PHP REST API FILE UPLOAD | |
// source: https://www.onlyxcodes.com/2021/03/php-rest-api-file-upload.html | |
header("Content-Type: application/json"); | |
header("Access-Control-Allow-Origin: *"); | |
header("Access-Control-Allow-Methods: POST"); | |
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization"); | |
$data = json_decode(file_get_contents("php://input"), true); // collect input parameters and convert into readable format |
This file contains hidden or 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
<form action="/create" method="POST"> | |
<label>Title</label> | |
<input type="text" name="title"> | |
<br> | |
<label>Description</label> | |
<input type="text" name="body"> | |
<br> | |
<input type="submit" value="SAVE"> | |
</form> |
This file contains hidden or 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
// Template engine | |
app.set('views', 'views'); | |
app.set('view engine', 'ejs'); | |
var bodyParser = require('body-parser'); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); |
This file contains hidden or 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 express = require("express") | |
const router = express.Router() | |
const Task = require('./models/Task') | |
// List of tasks | |
router.get('/', (req, res) => { | |
Task.find({}).exec((err, tasks) => { | |
res.send(tasks) | |
}) | |
}); |
This file contains hidden or 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 express = require("express") | |
const router = express.Router() | |
router.use('/', function(req, res) { | |
res.send('Hello World!') | |
}); | |
module.exports = router |
This file contains hidden or 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
// Route | |
const router = require('./router.js') | |
app.use('/', router) |
This file contains hidden or 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 mongoose = require('mongoose') | |
var TaskSchema = new mongoose.Schema({ | |
title: { | |
type: String, | |
required: true | |
}, | |
body: { | |
type: String, | |
default: '' |
This file contains hidden or 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
// Connecting database | |
DB_URL = 'mongodb://localhost:27017/todo' | |
mongoose.connect(DB_URL, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true }) | |
mongoose.connection.once('open', () => { | |
console.log('Connected to database'); | |
}).on('error', (error) => { | |
console.log(`There is an error in connecting database: ${error}`); | |
}); |
This file contains hidden or 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 express = require('express') | |
const app = express() | |
app.get('/', function(req, res) { | |
res.send('Hello World!') | |
}) | |
const port = 3000 | |
app.listen(port, () => { | |
console.log('Server started on port', port) |