搭建一个nodejs项目 使用express

初始化项目

新建一个文件夹,运行 npm init 初始化项目

mkdir wqs_node
cd wqs_node
npm init

按照提示输入一些项目的相关信息

D:\web\node>cd wqs_node


D:\web\node\wqs_node>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (okadaGo)
Sorry, name can no longer contain capital letters.
name: (okadaGo) okada_go
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\web\node\okadaGo\package.json:

{
  "name": "wqs_node",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes)

D:\web\node\wqs_node>

目录结构

建立三个文件夹:public、routes 和 views。

项目的文件结构如下

├─models/
├─public/
├─routes/
├─views/
├─index.js
└─package.json

对应文件及文件夹的用处:

  • models 存放操作数据库的文件
  • public 存放静态文件,如 css、图片等
  • routes 存放路由文件
  • views 存放模板文件
  • index.js 程序主文件
  • package.json 存储项目的信息,比如项目名、描述、作者、依赖等

安装依赖

安装 express 框架

npm install express --save

启动项目

进入项目的根目录,建立一个 index.js 文件,并加入如下代码

var express = require('express');
var app = express();

app.get('/', function(res, rep) {
    rep.send('Hello, word!');
});

app.listen(3000);

然后在控制台中输入

node index.js

接着使用浏览器访问 http://localhost:3000 ,即可访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值