我用路由和MySQL数据库创建了一个完整的NODEJS应用程序,它在localhost:3000上进行侦听。我已经写了一个智能合约,并在remix在线IDE上对其进行了测试。
智能合约:
pragma solidity ^0.4.11;
contract Hello {
string name;
function getName() constant returns(string){
return name;
}
function setName(string n) {
name = n;
}
}
我的app.js:
let createError = require('http-errors');
let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let logger = require('morgan');
let passport = require('passport');
let md5 = require('md5');
let LocalStrategy = require("passport-local");
let methodOverride = require("method-override");
let app = express();
let connection = require('./database');
let indexRouter = require('./routes/index');
let adminRouter = require('./routes/admin');
let studentRouter = require('./routes/student');
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride("_method"));
app.use(function(req, res, next) {
//make available inside our template
res.locals.currentUser = req.user;
//important: move to the code that handles the route
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/student', studentRouter);
app.use('/admin', adminRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我的admin.js
var express = require('express');
var router = express.Router();
let connection = require('../database');
let md5 = require('md5');
// gets admin page
router.get('/', function(req, res, next) {
console.log("----------------------------IN ADMIN ROUTER");
let queryString = "SELECT * FROM `semester` WHERE status =1 ;";
connection.query(queryString, function (err, result) {
if (err) {
res.render('admin/admin', {title: 'admin', bool: 0, data: " "});
}
if(result){
res.render('admin/admin', { title: 'admin', bool: 1 , data: result });
}
else{
res.render('admin/admin', { title: 'admin', bool :0 , data: " " });
}
});
});
router.get('/profile', function(req, res, next) {
res.render('admin/profile', { title: 'Profile' });
});
router.get('/viewStudentRecord', function(req, res, next) {
res.render('admin/viewStudentRecord', { title: 'View student record', bool: 0, data: " " });
});
现在我想在admin.js中调用智能合约 我看到了使用来自nodejs的智能合约的示例,但整个应用程序仅用于智能合约,它们是单页应用程序。
ps。我已经安装了GANACHE,METAMASK,WEB3