YC Yc.W home Systems · C++ · AI Infrastructure
Programming · Note

Node.js Error

Explore all Programming notes →

Node.js Error: Cannot find module express的解决办法

版本范围: Express 4.16.0(2017-09-28)起提供内置 express.json() 和 express.urlencoded(),Express 5 仍保留这两个 parser;更早的 4.x 版本使用 body-parser。

  1. 在工程目录安装 Express(不要把全局安装 Express 作为模块解析错误的解决方案),cmd 中执行:
    cd <项目目录>
    npm install express
    

    express-generator 是独立的命令行工具,如需使用它,应单独安装或运行。

  1. 如果仍报 Cannot find module,确认当前 JS 文件属于该工程,并从该工程目录执行 npm install express。 ———————————————— 版权声明:本文为CSDN博主「mlsama」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/mlsama/article/details/80211033

TypeError: Router.use() requires a middleware function but got a Object

写路由模块的时候忘记导出路由 module.exports = router

Router 传入全局变量

实现1:module.exports

index.js
var Dev = require('./Routes/Dev.js');
app.use('/Dev', Dev(app));

Router.js
var appObj = null;
module.exports = function(app){
//响应全局变量
appObj = app;
return router;
}

EasyUI DataGrid

方法loadData 加载空参数时

$('#TT').datagrid('loadData',{"total":0,"rows":[]})

参数queryParams使用

  • get 在req的query属性内能看到参数
  • post 在req的body属性内能看到参数
  • Error: 在使用 Router 时,如果 POST 请求的 req.body 为空,需要添加相应的 body parser。

    Express 4.16.0 及以上(包括 Express 5):

    var express = require('express');
    var router = express.Router();
    router.use(express.json());
    router.use(express.urlencoded({ extended: true }));
    

    Express 4.16.0 之前的 4.x:

    var express = require('express');
    var bodyParser = require('body-parser');
    var router = express.Router();
    router.use(bodyParser.json());
    router.use(bodyParser.urlencoded({ extended: true }));
    

在easyui DataGrid 中使用 linkbutton

如果直接在 field 的 formatter 中直接 return 一个linkbutton 按钮,则无法显示图标,只有普通的 a 标签,无法加载样式,因为easyui 只在页面加载时渲染标签,因此在使用formatter格式化列数据后,动态添加的linkbutton样式无法渲染样式,需要在 datagrid 加载完后进行重新渲染,使用 onLoadSuccess 进行对 linkbutton渲染 $(‘.classname’).linkbutton({text:”test”,plain:’true’,iconCls:’icon-add’}); ———————————————— 版权声明:本文为CSDN博主「宝贝一路向前」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/xcmonline/java/article/details/52944026

styler:cellStyler

	<table id="TT" class="easyui-datagrid" title="DataGrid Cell Style" style="width:700px;height:250px"
			data-options="
				singleSelect: true,
				iconCls: 'icon-save'
			">
		<thead>
			<tr>
				<th data-options="field:'itemid',width:80">Item ID</th>
				<th data-options="field:'productid',width:100">Product</th>
				<th data-options="field:'listprice',width:80,align:'right',styler:cellStyler">List Price</th>
				<th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>
				<th data-options="field:'attr1',width:250">Attribute</th>
				<th data-options="field:'status',width:60,align:'center'">Status</th>
			</tr>
		</thead>
	</table>
	<script type="text/javascript">
		$(function () {
			loadDevMonitorList();
		});
		function loadDevMonitorList(){
			$('#TT').datagrid('loadData',{"total":0,"rows":[{
			"itemid": 0,
			"listprice": "1"},
			{
			"itemid": 1,
			"listprice": "31"},
			{
			"itemid": 2,
			"listprice": "100"}]})
		};

		function cellStyler(value,row,index){
			if (value){
				return 'background-color:#ffee00;color:red;';
			}
		}
	</script>

注意,如果用脚本重新加载数据,要在加载的时候加上styler:cellStyler

 	$('#Info').datagrid({
        url:'D1/getD2',
        queryParams:{
            D1ID: D1Id
        },
        method:'post',
        fitColumns : true,//自动设置列的宽度
        singleSelect : true,//单选
        striped:true,//条纹
        //height:400,
        pagination:true,//翻页或滚动
        GridLines:'Horizontal',
        columns:[[
            {field:'T',title:'T',styler:cellStyler}
        ]]
    });

nodejs中req.body 为空的问题

WebSite

Express 4.16.0 起提供 express.json() 和 express.urlencoded();更早的 4.x 版本应安装 body-parser,并分别调用 bodyParser.json() 和 bodyParser.urlencoded()。require('body-parser') 返回的是对象,不能写成 bodyParser()。对于 URL-encoded 表单,请让请求的 Content-Type 与 urlencoded parser 匹配,例如 application/x-www-form-urlencoded;extended 选项应根据所需的表单语法选择。

参考:Express 安装文档、Node.js 模块解析、Express 4.16.0 发布记录、body-parser 1.18.2 README 和 Express 5 源码。


AI 修改声明: 本文由 LLM 协助校对,最近修改时间:2026-09-25 03:56(UTC+08:00)。


← Previous Node.js Forever