DBILITY

독거 가능성 100% 노후에 라면값이라도 하게 센스를 발휘합시다!😅
Please click on the ad so that I can pay for ramen in my old age!
点击一下广告,让老后吃个泡面钱吧!
老後にラーメン代だけでもするように広告を一回クリックしてください。

node express server basic 본문

nodejs

node express server basic

DBILITY 2021. 12. 15. 11:08
반응형

intellij를 사용해서 JavaScript->Node.js 프로젝트를 생성하면 npm init가 자동 실행된다.

Terminal을 열어 npm install express를 설치하고, nodemon도 설치해 준다.

다음은 express서버의 기초 코드로 nodemon server.js로 실행했다.

const express = require('express');
const app = express();
const port = process.env.PORT || 9090;
app.listen(port, () => {
    console.log(`this server is listening port ${port}`);
});

app.get('/hello',(req,res)=>{

    //res.json({message:'hello nodejs'});
    //res.send('<h4>hello nodejs</h4>');
    //res.sendStatus(200);
    res.sendFile(__dirname+'/package.json',(e)=>{
        console.log(e);
    });
});

이렇게 간편할 수가 있나...

간단한 테스트API는 즉시 작성해서 쓰기에 편하겠다.

반응형

'nodejs' 카테고리의 다른 글

node MongoDB connection URL special characters in the password string  (0) 2021.12.15
Comments