Get started with Node.JS in 2 minutes
- Install Node.JS
- Create an account with MongoDB Atlas
- Create Node.JS project
- npm init
- Install dependencies
- npm install --save express bcryptjs passport ejs express-ejs-layout mongoose connect-flash express-session
- Install nodemon to restart server on every save
- npm install -D nodemon
- Add to scripts in package.json the following
- "scripts":{"start":"node app.js", "dev":"nodemon app.js"}
- Create app.js file
- Add boiler plate express
- const express = require("express");
- const app = express();
- const PORT = process.env.PORT || 5000;
- app.listen(PORT, console.log(`Server started on PORT ${PORT}`));
- Run the following in terminal
- npm run dev
- Create folder routes
- Create two files inside routes
- index.js
- users.js
- Inside routes/index.js add the following
- const express = require("express");
- const router = express.Router();
- router.get('/', (req, res) => res.send("Welcome"));
- module.exports = router;
- Inside app.js add the following under const app = express();
- app.use('/', require('./routes/index'));
- Open localhost:5000 in browser
Comments
Post a Comment