Skip to content

This is a nodejs project for beginners, It contains the function of adding, deleting, update and query.

Notifications You must be signed in to change notification settings

lpcheniris/nodejs-beginner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to setup a nodejs project on mac

Tech stack:

  • Nodejs
  • Typescript
  • Express
  • Mongoodb
  • VSCode

Before start project, you should install on you mac:

  • Node,
  • Mongodb,
  • Git,

Now, we start it step by step

  1. install TS and express
mkdir  project-name  //Your project folder.
cd project-name   // open the folder.
npm init -y   // Init npm, and defult yes
npm install typescript -g --save-dev  // Install ts
npm install express body-parser --save-dev  //Install express and body-parse 
npm install @types/express --save-dev  
npx tsc -init   //add tsc command and add ts config file

  1. init git and add .gitignore
  • git init
  • add .gitignore for nodejs , you can use the file from github

3. Now, we start a server for our project

Create a src/index.ts

import express, { Application } from 'express';
import {json} from "body-parser";

const app: Application = express();
app.use(json())

const port = 8000;

// Start server
app.listen(port, () => console.log(`Server is listening on port ${port}!`));
  1. You nedd transfer ts to js by add command to package.json. Before this, you need install some npm packages.
npm install ts-node-dev --save-dev 
"scripts": {
   "start": "ts-node-dev src/index.ts"
}

Then, you can use "npm run start" command to run you project.

  1. Setup routes

First, create controll file, It's src/controlls/IndexControll.ts

import { NextFunction, Request, Response, Router } from 'express';

const IndexController: Router = Router();

IndexController.get('/', async (req: Request, res: Response, next: NextFunction) => {
    try {
        res.status(200).send({ data: 'Hello word!' });
    } catch (e) {
        next(e);
    }
});

export default IndexController

Second, add this code to index.ts, and import IndexController to index.ts

import IndexControllers from "./src/controllers/IndexControllers"

app.use("/", IndexControllers);

  1. How to debug a web nodejs in vscode
  • Click debug button in left side toolbar.
  • Click "create a launch.json", then choose nodejs. Now, + we have ./vscode/launch.json.
  • Click "run and debug" button, choose run command. This project is "npm run start".
  • Next, you can set break point for code in VScode.
  • When post http request to node server, the application be stoped on break point. You can see something in break point.

8 .Run database

you can use brew install mongodb or use other install ways

Run mongodb :

mongod --dbpath ~/mongodbData/cityinfodb --port 27018

It mean you setup mongodb server on port 27018, and the data be stored in ~/mongodbData/cityinfodb

Next , we can use mongo command create a database. Connet mongodb by "mongo —port 27018"

Then you can see the database by "db show", and create dababase by "use databaseName". Maybe you can't see it, that beacuse there are not data in this database.

  1. We can connect and use database by mongoose. First, install it .
npm install mongoose --save-dev

This is step is connect mongodb to our project

use this code to connect mongodb

import mongoose from "mongoose";

function setupMongo(): void {
  const mongoDB = "mongodb://localhost:27018/databasename";


  mongoose.connect(mongoDB, { useNewUrlParser: true });

  //Get the default connection
  var db = mongoose.connection;

  db.on("open", () => { console.log("Mongo connection successfully") })

  //Bind connection to error event (to get notification of connection errors)
  db.on('error', console.error.bind(console, 'MongoDB connection error:'));
}

export default setupMongo;

9.Next, we create schema. Schema is a data definition format. For example, user is the object in our project. And we can express it by schema.

import mongoose from "mongoose"  // import mongoose 

const UserSchema = new mongoose.Schema({   //Define schema.
 name: {type: String, required: true, minlength: 2}  //Define property that be named as "name", and it's required, type is String,minlength is 2.
})

export const User = mongoose.model("User", UserSchema)  //Transfer schema to models of monggose, and export it .

10 Create controller

// Now, we need to create a controller for reciving request and post responses.
import { Router, Request, Response, NextFunction, request, response } from "express"
import { User } from "../models/User"

const UserController: Router = Router()

UserController.post("/", async (req: Request, res: Response, next: NextFunction) => {
    try {
        const user = await new User(req.body).save();
        res.send({ data: user._id })
    } catch (err) {
        next(err)
    }
})

11 add route // This step is add route to our app, we can add route in index.ts

app.use("/", UserControllers);

12 Now, you can post request and get Response by postmen.

About

This is a nodejs project for beginners, It contains the function of adding, deleting, update and query.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published