forked from sonia145/extensions-101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
43 lines (31 loc) · 1.27 KB
/
backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Importing Libraries */
var express = require("express");
var bodyParser = require('body-parser');
var mongoose = require("mongoose");
/* Express Step 1: Creating an express application */
var app = express();
//set port
var port = 3000;
/* Express Step 2: Start Server */
app.listen(port, () => {
console.log("Server listening on port " + port);
});
// Express Step 3: Use body-parser library to help parse incoming request bodies
app.use(bodyParser.json());
/* Mongoose Step 1: Connecting to Mongoose */
mongoose.connect("mongodb://localhost:27017/WYR-test", {useNewUrlParser: true});
/* Mongoose Step 2: Define Model */
var Question = mongoose.model("Question", {
optionA: String,
optionB: String
});
/* This is included because its allows extenion to run external javascript.
If you are interested in learning more, check out: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS */
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
// Note that the origin of an extension iframe will be null
// so the Access-Control-Allow-Origin has to be wildcard.
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});