You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// expressモジュールをアプリケーションに追加importexpressfrom'express';// ポート番号の指定constPORT=process.env.PORT||8080;// expressアプリケーションをapp定数に代入constapp=express();// jsonオブジェクトとして認識できるようになるapp.use(express.json());// ドメインのルートにGet経路を設定するapp.get('/',(req,res)=>res.send('Express + TypeScript Server is work!'));// 指定したポートを監視するようにアプリケーションを設定するapp.listen(PORT,()=>{console.log(`[server]: Server is running at https://localhost:${PORT}`);});
express と mongodb を mongoose でつなげる
前提
mongodb は起動している
mongodb のポートはデフォルトのポート番号
DBはすでに生成されている
importexpressfrom'express';importmongoosefrom'mongoose';constDB_URL='mongodb://127.0.0.1:27017/';constDB_NAME='comic_impressions_exchange_db';constPORT=process.env.PORT||8080;constdbOption={useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false,useCreateIndex: true};mongoose.connect(`${DB_URL}${DB_NAME}`,dbOption);mongoose.Promise=global.Promiseconstdb=mongoose.connection;db.once('open',()=>{console.log('MongoDB open Success with Mongoose');});constapp=express();app.use(express.urlencoded({extended: false}));app.use(express.json());app.set('port',PORT);app.listen(app.get('port'),()=>{console.log(`[server]: Server is running at https://localhost:${app.get('port')}`);});