Node & Express学习笔记

安装插件
更新node
需要安装的插件:express,cors
npm install package-name

require():以一个本地文件路径作为参数。
cors能让浏览器和服务器免受安全限制,自由通信。

//Include the 'express' package in the project
const express = require('express');
//create an instance of an express app
const app = express();

//Include the 'cors' package in your project
//and connect it to the express app instance
const cors = require("cors");
app.use(cors());

服务端的文件结构不同于客户端,它将网页相关的文档放在一个目录下,如‘website’,server.js放在与这个文件夹同级的一层。
连接服务端代码与客户端:

//the browser code(index.html and any additional pages) housed in the website folder
app.use(express.static('website'));

使用Express创建一个本地服务器:

const port = 3000;
const server = app.listen(port, listening);
function listening(){
    // console.log(server);
    console.log(`running on localhost: ${port}`);
};

使用箭头函数:

//Create a server with a callback function
const server = app.listen(port, ()=>{
    console.log("the port number:" + port);
});

在终端运行文件

node server.js
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值