- MEAN.JS 教程
- MEAN.JS - 首页
- MEAN.JS - 概述
- MEAN.JS - 架构
- 构建 Node Web 应用
- MEAN.JS - MEAN 项目设置
- 构建静态路由 Node Express
- MEAN.JS - 构建数据模型
- MEAN.JS - REST API
- 使用 Angular 的前端
- 应用中的 Angular 组件
- 使用 Angular 构建单页应用
- 构建 SPA:进阶
- MEAN.JS 有用资源
- MEAN.JS - 快速指南
- MEAN.JS - 有用资源
- MEAN.JS - 讨论
MEAN.JS - MEAN 项目设置
本章包括创建和设置 MEAN 应用程序。我们使用 NodeJS 和 ExpressJS 结合起来创建项目。
先决条件
在开始创建 MEAN 应用程序之前,我们需要安装必要的先决条件。
您可以访问 Node.js 网站 Node.js 安装最新版本的 Node.js(适用于 Windows 用户)。下载 Node.js 时,npm 将自动安装到您的系统中。Linux 用户可以使用此 链接 安装 Node 和 npm。
使用以下命令检查 Node 和 npm 的版本:
$ node --version $ npm --version
命令将显示如下所示的版本:
创建 Express 项目
使用 mkdir 命令创建项目目录,如下所示:
$ mkdir mean-demo //this is name of repository
以上目录是 Node 应用程序的根目录。现在,要创建 package.json 文件,请运行以下命令:
$ cd webapp-demo $ npm init
init 命令将引导您完成创建 package.json 文件的过程:
此实用程序将引导您完成创建 package.json 文件的过程。它只涵盖最常见的项目,并尝试猜测合理的默认值。
See `npm help json` for definitive documentation on these fields and exactly what they do.
Use `npm install --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: (mean-demo) mean_tutorial
version: (1.0.0)
description: this is basic tutorial example for MEAN stack
entry point: (index.js) server.js
test command: test
git repository:
keywords: MEAN,Mongo,Express,Angular,Nodejs
author: Manisha
license: (ISC)
About to write to /home/mani/work/rnd/mean-demo/package.json:
{
"name": "mean_tutorial",
"version": "1.0.0",
"description": "this is basic tutorial example for MEAN stack",
"main": "server.js",
"scripts": {
"test": "test"
},
"keywords": [
"MEAN",
"Mongo",
"Express",
"Angular",
"Nodejs"
],
"author": "Manisha",
"license": "ISC"
}
Is this ok? (yes) yes
点击“是”,将生成如下所示的文件夹结构:
-mean-demo -package.json
package.json 文件将包含以下信息:
{
"name": "mean_tutorial",
"version": "1.0.0",
"description": "this is basic tutorial example for MEAN stack",
"main": "server.js",
"scripts": {
"test": "test"
},
"keywords": [
"MEAN",
"Mongo",
"Express",
"Angular",
"Nodejs"
],
"author": "Manisha",
"license": "ISC"
}
现在,要将 Express 项目配置到当前文件夹并安装框架的配置选项,请使用以下命令:
npm install express --save
转到您的项目目录并打开 package.json 文件,您将看到以下信息:
{
"name": "mean_tutorial",
"version": "1.0.0",
"description": "this is basic tutorial example for MEAN stack",
"main": "server.js",
"scripts": {
"test": "test"
},
"keywords": [
"MEAN",
"Mongo",
"Express",
"Angular",
"Nodejs"
],
"author": "Manisha",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
在这里您可以看到 express 依赖项已添加到文件中。现在,项目结构如下所示:
-mean-demo --node_modules created by npm install --package.json tells npm which packages we need --server.js set up our node application
运行应用程序
导航到您新创建的项目目录,并创建一个包含以下内容的 server.js 文件。
// modules =================================================
const express = require('express');
const app = express();
// set our port
const port = 3000;
app.get('/', (req, res) ⇒ res.send('Welcome to Tutorialspoint!'));
// startup our app at https://:3000
app.listen(port, () ⇒ console.log(`Example app listening on port ${port}!`));
接下来,使用以下命令运行应用程序:
$ npm start
您将获得如下所示的确认信息:
它提示 Express 应用程序正在运行。打开任何浏览器,并使用 **https://:3000** 访问应用程序。您将看到如下所示的“欢迎来到 Tutorialspoint!”文本:
广告