박기홍

added schemes, router

...@@ -2,67 +2,20 @@ const express = require('express'); ...@@ -2,67 +2,20 @@ const express = require('express');
2 const app = express(); 2 const app = express();
3 const port = 3000; 3 const port = 3000;
4 4
5 -let questions = []; 5 +const mongoose = require('mongoose');
6 -let jobList = []; 6 +const bodyParser = require('body-parser');
7 7
8 -app.get('/', (req, res, next) => { 8 +app.use(bodyParser.urlencoded({extended:true}));
9 - res.send('hello world!'); 9 +app.use(bodyParser.json());
10 -});
11 -
12 -app.get('/getQuestions', (req, res, next) => {
13 -
14 -
15 - /**
16 - * 질문 폼
17 - * 번호 질문내용 답1 답2 가중치1, 가중치2
18 - */
19 10
20 - let questionNums = []; 11 +const db = mongoose.connection;
21 - const numOfQuestions = 25; 12 +db.on('error', console.error);
22 - for(let i=0; i<numOfQuestions; i++){ 13 +db.once('open', function(){
23 - questionNums[i] = i; 14 + // CONNECTED TO MONGODB SERVER
24 - } 15 + console.log("Connected to mongod server");
25 - // shuffle(questionNums);
26 - questionNums.sort(function () {
27 - return Math.round(Math.random()) - 0.5
28 - });
29 -
30 - questionNums = questionNums.slice(0, 10);
31 -
32 - let sendQuestions = [];
33 - for(let i=0; i<10; i++){
34 - sendQuestions.push(questions[questionNums[i]]);
35 - }
36 - res.send(sendQuestions);
37 }); 16 });
38 17
39 -app.post('/submit', (req, res, next) => { 18 +mongoose.connect('mongodb://localhost/khusat');
40 - const {answer} = req.body;
41 - console.log(answer);
42 -
43 - const answerArray = [];
44 -
45 - let score = 50;
46 -
47 - /**
48 - * 정답 폼
49 - * 문항 번호 정답 고른것
50 - */
51 -
52 - for(let i=0; i< answerArray.length; i++){
53 - if(answerArray[i][1] === 0){
54 - // 1번답 골랐을때
55 - score += questions[answerArray[i]][3];
56 - }
57 - else{
58 - score += questions[answerArray[i]][4];
59 - }
60 - }
61 -
62 - const recommendedJob = jobList[score%jobList.length];
63 -
64 - res.send(recommendedJob);
65 -});
66 19
67 app.listen(port, () => { 20 app.listen(port, () => {
68 console.log(`Server is running at ${port}`); 21 console.log(`Server is running at ${port}`);
......
1 +const mongoose = require('mongoose');
2 +const schema = mongoose.Schema;
3 +
4 +const jobSchema = new schema({
5 + /**
6 + * 특기병 폼
7 + * 번호, 대분류, 중분류, 소분류, 설명, 사진
8 + */
9 +
10 + num:{
11 + type: Number,
12 + default: -1
13 + },
14 + high:{
15 + type: String,
16 + default: "대분류를 입력하세요",
17 + },
18 + middle:{
19 + type: String,
20 + default: "중분류를 입력하세요",
21 + },
22 + low:{
23 + type: String,
24 + default: "소분류를 입력하세요",
25 + },
26 + description:{
27 + type: String,
28 + default: "보직 설명",
29 + },
30 + image:{
31 + type: String,
32 + default: "???/???"
33 + }
34 +
35 +});
36 +
37 +module.exports = mongoose.model('job', jobSchema);
...\ No newline at end of file ...\ No newline at end of file
1 +const mongoose = require('mongoose');
2 +const schema = mongoose.Schema;
3 +
4 +const questionSchema = new schema({
5 + /**
6 + * 질문 폼
7 + * 번호 질문내용 답1 답2 가중치1, 가중치2
8 + */
9 +
10 + questionNum:{
11 + type: Number,
12 + default: -1
13 + },
14 + question: {
15 + type: String,
16 + default: "질문",
17 + },
18 + answer1: {
19 + type: String,
20 + default: "그렇다"
21 + },
22 + answer2: {
23 + type: String,
24 + default: "아니다"
25 + },
26 + score1: {
27 + type: Number,
28 + default: 50,
29 + },
30 + score2: {
31 + type: Number,
32 + default: -50,
33 + },
34 +});
35 +
36 +module.exports = mongoose.model('question', questionSchema);
...\ No newline at end of file ...\ No newline at end of file
1 +const Question = require('../models/questions');
2 +const Job = require('../models/jobs');
3 +
4 +
5 +module.exports = function(app)
6 +{
7 +
8 + // let questions = [];
9 + // let jobList = [];
10 +
11 + app.get('/', (req, res, next) => {
12 + res.send('hello world!');
13 + });
14 +
15 + app.get('/getQuestions', async (req, res, next) => {
16 +
17 +
18 + /**
19 + * 질문 폼
20 + * 번호 질문내용 답1 답2 가중치1, 가중치2
21 + */
22 +
23 + const questions = await Question.find().exec();
24 +
25 + let questionNums = [];
26 + const numOfQuestions = 25;
27 + for(let i=0; i<numOfQuestions; i++){
28 + questionNums[i] = i;
29 + }
30 + // shuffle(questionNums);
31 + questionNums.sort(function () {
32 + return Math.round(Math.random()) - 0.5
33 + });
34 +
35 + questionNums = questionNums.slice(0, 10);
36 +
37 + let sendQuestions = [];
38 + for(let i=0; i<10; i++){
39 + sendQuestions.push(questions[questionNums[i]]);
40 + }
41 + res.send(sendQuestions);
42 + });
43 +
44 + app.post('/submit', async (req, res, next) => {
45 + const {answer} = req.body;
46 + console.log(answer);
47 +
48 + const answerArray = await Job.find().exec();
49 +
50 + let score = 50;
51 +
52 + /**
53 + * 정답 폼
54 + * 문항 번호 정답 고른것
55 + */
56 +
57 + for(let i=0; i< answerArray.length; i++){
58 + if(answerArray[i][1] === 0){
59 + // 1번답 골랐을때
60 + score += questions[answerArray[i]][3];
61 + }
62 + else{
63 + score += questions[answerArray[i]][4];
64 + }
65 + }
66 +
67 + const recommendedJob = jobList[score%jobList.length];
68 +
69 + res.send(recommendedJob);
70 + });
71 +
72 +}
...\ No newline at end of file ...\ No newline at end of file