zoukankan      html  css  js  c++  java
  • node+express的html页面访问

    node+express访问himl文件的入门实例

    1.首先在需要操作的文件夹下安装express模块

      D:vs codeFilehrmlmysqlweblod> npm install express 

    2.创建index.js文件,如下

     1 //引入exprexx模块
     2 var express =require("express");
     3 var app =express();
     4 app.use(express.static('public'))
     5 
     6 //参数‘/’可当作设置url的根显示页面,这里即”http://localhost:3000/“访问的页面设置为index.html
     7 app.get('/',(req,res)=>{
     8     res.sendFile(__dirname+"/"+"index.html")        //设置/ 下访问文件位置
     9 });
    10 
    11 //参数‘/tow’可当作设置url的/tow下显示页面,这里即”http://localhost:3000/tow“访问的页面设置为index2.html
    12 app.get("/tow",(req,res)=>{
    13     res.sendFile(__dirname+"/"+"index2.html")        //设置/tow下访问文件位置
    14 })
    15 
    16 //设置端口3000访问地址,即http://localhost:3000
    17 var server =app.listen(3000,()=>{
    18     var port =server.address().port
    19     console.log("【】访问地址http://localhost:%s",port)
    20 })

    3.创建index.html和index2.html测试文件;

    index.html

    <p>Holle word</p>

    index.html

    <meta charset="utf-8">
    <p>我是index2</p>

    4.测试

    在当前文件目录终端下输入:node. index.js

      D:vs codeFilehrmlmysqlweblod> node index.js 

    访问 http://localhost:3000 显示

    Holle word

    访问 http://localhost:3000/tow 显示

    我是index2
  • 相关阅读:
    springMVC后端返回数据到前端
    spring MVC配置
    SSM框架中配置静态资源加载
    js实践问题收集日记
    页面HTml学习笔记
    js页面传值实践
    struts2中jsp页面与action之间的传值
    json与Java对象的转换
    JDBC的简单应用
    新的开始,重新启用博客园
  • 原文地址:https://www.cnblogs.com/hmcjsc/p/12704350.html
Copyright © 2011-2022 走看看