teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

app.use を追加

2021/06/20 10:52

投稿

hiro_ike
hiro_ike

スコア48

title CHANGED
File without changes
body CHANGED
@@ -69,6 +69,8 @@
69
69
 
70
70
  app.set('view engine', 'jsx');
71
71
  app.engine('jsx', require('express-react-views').createEngine())
72
+ //追記
73
+ app.use(express.urlencoded({ extended: true }))
72
74
 
73
75
  app.get('/', (req, res) => {
74
76
  res.send('Welcome to the Pokemon App!')

3

不要なコメント削除

2021/06/20 10:52

投稿

hiro_ike
hiro_ike

スコア48

title CHANGED
File without changes
body CHANGED
@@ -75,12 +75,9 @@
75
75
  })
76
76
 
77
77
  /*
78
- Index
78
+ Indexにも追記
79
79
  */
80
80
  app.get('/pokemon', (req, res) => {
81
- // res.render('Index', {Pokemon: Pokemon});
82
- // })
83
- // app.get('/fruits/', (req, res) => {
84
81
  Pokemon.find({}, (err, foundPokemon)=>{
85
82
  if(err){
86
83
  res.status(404).send({

2

追記してみました

2021/06/20 08:42

投稿

hiro_ike
hiro_ike

スコア48

title CHANGED
File without changes
body CHANGED
@@ -54,6 +54,8 @@
54
54
  const app = express();
55
55
  const PORT = 3000;
56
56
  const Pokemon = require('./models/pokemon');
57
+
58
+
57
59
  const mongoose = require('mongoose');
58
60
  mongoose.connect(process.env.MONGO_URI, {
59
61
  useNewUrlParser: true,
@@ -72,10 +74,29 @@
72
74
  res.send('Welcome to the Pokemon App!')
73
75
  })
74
76
 
77
+ /*
78
+ Index
79
+ */
75
80
  app.get('/pokemon', (req, res) => {
76
- res.render('Index', {Pokemon: Pokemon});
81
+ // res.render('Index', {Pokemon: Pokemon});
82
+ // })
83
+ // app.get('/fruits/', (req, res) => {
84
+ Pokemon.find({}, (err, foundPokemon)=>{
85
+ if(err){
86
+ res.status(404).send({
87
+ msg: err.message
88
+ })
89
+ } else {
90
+ res.render('Index', {
91
+ Pokemon: foundPokemon
92
+ })
93
+ }
94
+ })
95
+
77
96
  })
78
97
 
98
+ /*
99
+ */
79
100
  app.get('/pokemon/new', (req, res) => {
80
101
  res.render('New')
81
102
  })
@@ -86,8 +107,24 @@
86
107
  });
87
108
  })
88
109
 
110
+ /*
111
+ 追記
112
+ */
113
+ app.post('/pokemon', (req, res) =>{
114
+ Pokemon.create(req.body, (err, createdPokemon ) => {
115
+ if(err){
116
+ res.status(404).send({
117
+ msg: err.message
118
+ })
119
+ } else {
120
+ console.log(createdPokemon);
121
+ res.redirect('/pokemon')
122
+ }
123
+ })
124
+ })
125
+
89
126
  app.listen(PORT, () => {
90
- console.log('connected', PORT)
127
+ console.log('We in the building', PORT)
91
128
  })
92
129
  ```
93
130
 

1

MongoDB 追加

2021/06/20 08:36

投稿

hiro_ike
hiro_ike

スコア48

title CHANGED
File without changes
body CHANGED
File without changes