当前位置: 首页 > news >正文

网站关键词几个知识搜索引擎

网站关键词几个,知识搜索引擎,品质好物推荐怎么上,台州网站建设方案推广Node.js 是一个基于 Chrome V8 JavaScript 引擎构建的 JavaScript 运行时环境。简单来说,Node.js 让 JavaScript 可以在服务器端运行,而不仅仅局限于浏览器中。 创建第一个 Node.js 项目 在项目文件夹执行npm init -y 会出现package.json 然后新建ap…

Node.js 是一个基于 Chrome V8 JavaScript 引擎构建的 JavaScript 运行时环境。简单来说,Node.js 让 JavaScript 可以在服务器端运行,而不仅仅局限于浏览器中。

创建第一个 Node.js 项目

在项目文件夹执行npm init -y

会出现package.json

然后新建app.js

输入测试代码:

const http = require('http');const server = http.createServer((req,res)=>{res.statusCode = 200;res.setHeader('Content-Type','text/plain');res.end('RUNOOB Node Test ~ hello,Node js\n');
});const port = 3000;
server.listen(port,()=>{console.log(`服务器运行地址:http://localhost:${port}/`);
})

访问3000,打印了

Nodejs包含哪几部分

1.使用 require 指令来加载和引入模块

const module = require('module-name');

2.创建服务器

使用http.createServer()创建,

listen方法监听

函数通过request,response接收和响应

简便写法:

var http = require('http');http.createServer(function (request, response) {response.writeHead(200, {'Content-Type': 'text/plain'});response.end('Hello World\n');
}).listen(8888);console.log('Server running at http://127.0.0.1:8888/');

Nodejs路由控制

const http = require('http');const server = http.createServer((req,res)=>{if(req.url === '/'){res.writeHead(200,{"content-type":'text/plain'});res.end("welcome ");}else if(req.url === '/about'){res.writeHead(200,{"content-type":'text/plain'});res.end("about");}else{res.writeHead(404,{"content-type":'text/plain'});res.end('404 Not Found');}
});const port = 3000;
server.listen(port,()=>{console.log(`服务器运行地址:http://localhost:${port}/`);
})

node实现打包工具webpack

蓝桥杯要求手写一个 Webpack,这是一道考察前端构建原理的综合题。

Webpack 本质是一个模块打包工具,它的核心工作流程包括:

  1. 解析入口文件:从入口文件开始,递归分析所有依赖。
  2. 构建依赖图:记录每个模块的依赖关系和源代码。
  3. 转换模块代码:通过 Loader 处理不同类型的文件(如 CSS、图片)。
  4. 打包输出:将所有模块合并为一个或多个 bundle 文件。
const fs = require('fs');
const path = require('path');
const babel = require('@babel/core');class MyWebpack {constructor(config) {this.config = config;this.entry = config.entry; // 入口文件路径this.output = config.output; // 输出配置this.modules = []; // 存储所有模块信息}// 启动打包run() {this.buildModule(this.entry, true);this.emitFiles();}// 构建模块buildModule(filename, isEntry) {const moduleId = './' + path.relative(process.cwd(), filename);const source = fs.readFileSync(filename, 'utf-8');const { dependencies, transformedSource } = this.parseModule(source, filename);const module = {id: moduleId,filename,dependencies,source: transformedSource};this.modules.push(module);// 递归处理依赖dependencies.forEach(dep => {this.buildModule(path.join(path.dirname(filename), dep));});return module;}// 解析模块:转换代码并提取依赖parseModule(source, filename) {const dependencies = [];// 使用 Babel 转换 ES6+ 代码为 CommonJS 格式const { code } = babel.transformSync(source, {presets: ['@babel/preset-env'],plugins: [[// 自定义插件:分析 import 语句收集依赖function() {return {visitor: {ImportDeclaration(path) {dependencies.push(path.node.source.value);}}};}]]});return { dependencies, transformedSource: code };}// 生成输出文件emitFiles() {const outputPath = path.join(this.output.path, this.output.filename);// 创建模块映射函数const modules = this.modules.reduce((acc, module) => {acc[module.id] = `function(module, exports, require) {${module.source}}`;return acc;}, {});// 生成自执行的 bundle 代码const bundle = `(function(modules) {// 模块缓存const installedModules = {};// 自定义 require 函数function require(id) {if (installedModules[id]) {return installedModules[id].exports;}const module = installedModules[id] = {exports: {},id: id,loaded: false};// 执行模块函数modules[id].call(module.exports,module,module.exports,require);module.loaded = true;return module.exports;}// 从入口模块开始执行return require('${this.entry}');})({${Object.entries(modules).map(([id, fn]) => `"${id}": ${fn}`).join(',')}});`;// 写入输出文件fs.writeFileSync(outputPath, bundle);console.log(`Bundle created at ${outputPath}`);}
}module.exports = MyWebpack;

使用express构建node项目

1.npm install express

2.npx express-generator,就自动创建好了

3.基本路由

  • app is an instance of express.
  • METHOD is an HTTP request method, in lowercase.
  • PATH is a path on the server.
  • HANDLER is the function executed when the route is matched.

app.method('path',handler) 

app.get('/', (req, res) => {res.send('Hello World!')
})

4.利用 Express 托管静态文件

为了提供诸如图像、CSS 文件和 JavaScript 文件之类的静态文件,请使用 Express 中的 express.static 内置中间件函数。

此函数特征如下:

express.static(root, [options])

例如,通过如下代码就可以将 public 目录下的图片、CSS 文件、JavaScript 文件对外开放访问:

app.use(express.static('public'))

现在,你就可以访问 public 目录中的所有文件了:

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

要为 express.static 函数服务的文件创建虚拟路径前缀(该路径在文件系统中不存在),请为静态目录指定挂载路径,如下所示:

<span style="background-color:#f7f7f7"><span style="color:black"><code class="language-javascript">app<span style="color:#999999">.</span><span style="color:#dd4a68">use<span style="color:#999999">(</span></span><span style="color:#669900">'/static'</span><span style="color:#999999">,</span> express<span style="color:#999999">.</span><span style="color:#0077aa">static</span><span style="color:#999999">(</span><span style="color:#669900">'public'</span><span style="color:#999999">)</span><span style="color:#999999">)</span>
</code></span></span>

现在,你就可以通过带有 /static 前缀地址来访问 public 目录中的文件了。

http://localhost:3000/static/images/kitten.jpg
http://localhost:3000/static/css/style.css
http://localhost:3000/static/js/app.js
http://localhost:3000/static/images/bg.png
http://localhost:3000/static/hello.html

 更多例子Express examples

http://www.dtcms.com/a/616483.html

相关文章:

  • 苏州 网站建设 app互联网行业分析
  • 卖主机网站网址导航浏览器下载
  • 网站建设项目运营岗晋城市网站建设管理人员
  • 网站建设 淘宝客末班wordpress 整体搬家
  • 广州网站建设互广网站建设费用初步预算
  • 自助游戏充值网站怎么做中国建筑怎么样
  • 网站策划建站永久网站建设教程
  • 邳州网站网站建设河南省建设厅代建中心
  • 网站建设公司郴州国家级示范职业学校 建设网站
  • 班级网站模板wordpress商务套餐
  • 国家网站建设的相关规定免费的空间网站
  • 淘宝客网站域名备案吗大型购物网站建设方案
  • 南召网站建设建设标准下载网站
  • wordpress只能传2m沈阳百度快照优化公司
  • 在线做网站有哪些平台360网页怎么制作
  • 哪些网站适合用自适应网易游戏推广代理加盟
  • 上海做网站设计公司公司网站建设怎么入账
  • 百度推广 网站建设兴城做网站推广的
  • 茶叶淘宝店网站建设ppt河北通信网站建设
  • 会议平台网站建设网站建设设计未来前景
  • 如何让客户做网站简单个人网站制作流程
  • 青岛php网站建设网站开发服务单位
  • 北海做网站公司网站聊天工具代码
  • 网站制作尺寸软件工程师报名官网
  • 网站式登录页面模板下载小程序开发教程资料
  • joomla 做的网站网络舆情事件
  • 做房地产用什么网站好深圳龙岗企业网站建设
  • 在线书店网站怎么做公司logo设计含义
  • 服务网点网站建设百度关键词推广条件
  • seo网站外链工具网站开发公司网站